<?php

class BtBatterysController extends RController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
    public $defaultAction = 'admin';
	public $layout='//layouts/lay_column2';
    public $menu=array();
    public $moduleName="运行分类报表";	//此处修改本模块名称
    
    public $province_id ;
    public $city_id ;
    public $county_id  ;
    public $base_id  ;
    public $level  =-1;
    public $cond_unit =-1;
    public $group_option = -1;
    public $option_id = -1 ;
    
    //tag for visible
    public $maker_visible_tag = false ;
    public $guige_visible_tag = false ;
    public $unit_visible_tag = false ;
    public $time_visible_tag = false ;
    public $muti_tag =-1;
    
    public $manufacturer;
    public $bt_type  ;
    public $time  ;
    public $start_time  ;
    public $end_time  ;
	public $searchQuery = '';
    
	
	
	public $select_unit = '';
	public $select_maker = '';
	public $select_size =  '';
	public $select_timer =  '';
	
	
	//modified by hyq 2013.3.16
	private	$tables = "
			From mtsys_base AS b 
			join mtsys_city AS c ON b.city_id = c.city_id
			JOIN mtsys_county AS cn ON b.county_id  = cn.county_id 
			JOIN mtsys_province AS p ON b.province_id  = p.province_id 
			JOIN bt_batterys AS bt ON b.BASE_id = bt.BASE_n
			JOIN dict_battery AS db ON bt.BATTERYTYPE_V = db.record_id
			JOIN dict_manufacturer AS dm ON db.MANCODE_V  = dm.record_id 
			";
		
	private	$sql_fields="
			SELECT @row := @row +1 AS id, g_table.*  from(
			select  bt.record_id bt_id,
			b.province_id,b.city_id,b.county_id, b.base_id,
			p.province_name,c.city_name,cn.county_name,b.base_name,
			db.record_id type_id,dm.record_id man_id,
			dm.manname_v manname,db.BATTERYTYPE_V  ,bt.BATTERYNUM_N,year(bt.SETUP_D) SETUP_D,
			ifnull(cr.red_n,0)  red_n , 
			ifnull(cy.yellow_n,0)yellow_n, ifnull(cg.green_n,0)green_n	
			";
		
	private	$sql_count="
			SELECT count(*)	from(
			SELECT count(*) 
			";
		
	private	$sql_details_table="
			SELECT @row := @row +1 AS id ,
                        p.province_name,c.city_name,cn.county_name,b.base_name,
                        dm.MANNAME_V,dbs.SORTNAME_V,bt.GROUPNAME_V, bt.record_id, 
                        bt.SETUP_D as bt_date,bt.STATUS_N ,db.BATTERYTYPE_V 
			FROM mtsys_base AS b
			join mtsys_city AS c ON b.city_id = c.city_id
			JOIN mtsys_county AS cn ON b.county_id  = cn.county_id 
			JOIN mtsys_province AS p ON b.province_id  = p.province_id 
			JOIN bt_batterys AS bt ON b.BASE_id = bt.BASE_n
            JOIN dict_battery AS db ON bt.BATTERYTYPE_V = db.record_id
			JOIN dict_manufacturer AS dm ON db.MANCODE_V  =dm.record_id
            JOIN dict_battery_sort AS dbs ON db.BATTERYSORT_V = dbs.RECORD_ID,
            (SELECT @row :=0)r where
			 ";
	private	$count_details_table ="
			SELECT count(*)
			FROM mtsys_base AS b
			join mtsys_city AS c ON b.city_id = c.city_id
			JOIN mtsys_county AS cn ON b.county_id  = cn.county_id 
			JOIN mtsys_province AS p ON b.province_id  = p.province_id 
			JOIN bt_batterys AS bt ON b.BASE_id = bt.BASE_n
            JOIN dict_battery AS db ON bt.BATTERYTYPE_V = db.record_id
			JOIN dict_manufacturer AS dm ON db.MANCODE_V  =dm.record_id
            JOIN dict_battery_sort AS dbs ON db.BATTERYSORT_V = dbs.RECORD_ID,
            (SELECT @row :=0)r where ";
	
	private $state_sql = "
			(SELECT  count(1) as %s,  %s  
			From mtsys_base AS b
			JOIN bt_batterys AS bt ON b.BASE_id = bt.BASE_n
			JOIN dict_battery AS db ON bt.BATTERYTYPE_V = db.record_id
			JOIN dict_manufacturer AS dm ON db.MANCODE_V = dm.record_id where bt.status_n = %d  and " ;
	
	private $condition_sql = "GROUP BY %s ) as %s  on  %s = %s" ;
	
    //status condition for each battery
    public $status=1;
    //public $group_option = -1;
    
    
 //CONST
    private $YELLOW = 1;
    private $RED = 2;
    private $GREEN = 0;
    
    private $GROUP_BY_NOTHING = 0;
    private $GROUP_BY_MANUFACTURER = 1;
    private $GROUP_BY_SIZE = 2;
    private $GROUP_BY_TIME = 3;
    private $GROUP_BY_PROVINCE = 10;
    private $GROUP_BY_CITY = 11;
    private $GROUP_BY_COUNTY = 12;
    private $GROUP_BY_BASE = 13;
    
    private $LEVEL_PROVINCE = 1;
    private $LEVEL_CITY = 2;
    private $LEVEL_COUNTY  = 3;
    private $LEVEL_BASE  = 4;
    public function init()
	{
    	$leftmenu = 'tongji';	//此处修改模块所属菜单
		$this->menu = Yii::app()->params[$leftmenu];
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
	
				'PROVINCE'=>array(self::BELONGS_TO, 'MtsysProvince', 'PROVINCE_ID'),
				'CITY'=>array(self::BELONGS_TO, 'MtsysCity', 'CITY_ID'),
				'COUNTY'=>array(self::BELONGS_TO, 'MtsysCounty', 'COUNTY_ID'),
				'BASE'=>array(self::BELONGS_TO, 'MtsysBase', 'BASE_N'),
					
		);
		/*
		 * return array(
					'Counties'=>array(self::BELONGS_TO, 'MtsysCounty', 'BASE_ID'),
	
			);*/
	}
	
	
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','getCitys','getCountys','charts'),
				'users'=>array('@'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
				array('allow', // allow admin user to perform 'admin' and 'delete' actions
						'actions'=>array('admin','details','Importdata', 'chart'),
						'users'=>array('admin'),
				),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		////echo $id;
		//var_dump($this->loadModel($id));
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionColor()
	{
		
		$dataProvider_details ;
		$dataProvider;
		$group_option = -1; // -1 : no muti; 0 : unit ; 1 :manufacturer; 2 :size;
		$sql_count;
		$sql;
		$greenSql;
		$yellowSql;
		$redSql;
		$sqlCondition = '';
		$sqlGroup = '';
		
		$sql_details=$this->sql_details_table;
		$count_details = $this->count_details_table;

		$str_unit ;
		$manufacturer;
		$size;
		$str_time;
		$status;
		//获取用户所属单位的级别
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$user_unit_level = $user->unit_level;
		$user_unit_id = $user->unit_id;
		if(isset($_GET['redirect']))
		{
			$str_unit =$_GET['select_unit'];
			$manufacturer = $_GET['select_maker'];
			$size = $_GET['select_size'];
			$str_time = $_GET['select_timer'];
			//echo 'redirect';
		
		}else {
			$str_unit =$_POST['select-unit'];
			$manufacturer = $_POST['select-manufacturer'];
			$size = $_POST['select-size'];
			$str_time = $_POST['select-timer'];
		}
		$this->select_unit = $str_unit;
		$this->select_maker = $manufacturer;
		$this->select_size = $size;
		$this->select_timer = $str_time;
		////echo 'unit'.$_POST['select-unit'];
		//$str_unit =$_POST['select-unit'];
		//$str_unit ="14,3,1|14,4,3";
		////echo 	$str_unit."str_unit".strlen($str_unit);
		if(isset($str_unit)){
			////echo "$str_unit=".$str_unit;
			$this->select_unit = $str_unit;
			if ($str_unit == null || strlen($str_unit) ==0  ) {
				////echo 	$this->group_option."level";
				$no_unit = 1;
				if($user_unit_level >= $this->LEVEL_PROVINCE ){
					if($user_unit_level == $this->LEVEL_PROVINCE){
						$this->level =$this->LEVEL_PROVINCE; 
						$this->province_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_PROVINCE;
					}elseif($user_unit_level == $this->LEVEL_CITY){
						$this->level =$this->LEVEL_CITY;
						$this->city_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_CITY;
					}elseif($user_unit_level == $this->LEVEL_COUNTY){
						$this->level =$this->LEVEL_COUNTY;
						$this->county_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_COUNTY;
					}elseif($user_unit_level == $this->LEVEL_BASE){
						/*
						$this->level =$this->LEVEL_BASE;
						$this->base_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_BASE;*/
					}
				}
			}else{

			$this->unit_visible_tag = true;	
			//split and split the unit string
			$units = explode("|", $str_unit);
			$numbers = count($units); //count 
			
			$all_units = explode(",", $units[0]);
			$this->level = count($all_units); //count the level
 
			$this->province_id = "";
			$this->city_id = "";
			$this->county_id = "";	
			$this->base_id = "";
			
			//parse the units_str
			foreach ($units as $unit){
				//echo $unit;
				$all_units = explode(",", $unit);
				$this->level = count($all_units); //count the level
				if($this->level == $this->LEVEL_PROVINCE){ //province
					$this->province_id .= ','.$all_units[0];
				}elseif($this->level == $this->LEVEL_CITY){ //city
					$this->province_id .= ','.$all_units[0];
					$this->city_id .= ','.$all_units[1];
				}elseif($this->level == $this->LEVEL_COUNTY){ //county
					$this->province_id .= ','.$all_units[0];
					$this->city_id .= ','.$all_units[1];
					$this->county_id .= ','.$all_units[2];
				}else if($this->level == $this->LEVEL_BASE){ //base
					$this->province_id .= ','.$all_units[0];	
					$this->city_id .= ','.$all_units[1];
					$this->county_id .= ','.$all_units[2];
					$this->base_id .= ','.$all_units[3];
				}				
			}
			
			$this->province_id = substr($this->province_id, 1, strlen($this->province_id)-1); 
			$this->city_id = substr($this->city_id, 1, strlen($this->city_id)-1); 
			$this->county_id = substr($this->county_id, 1, strlen($this->county_id) -1 ); 
			$this->base_id = substr($this->base_id, 1, strlen($this->base_id)-1); 
			}
		}

			if($this->level == $this->LEVEL_PROVINCE){
				//省级比较		
				//echo "LEVEL_PROVINCE";	
				if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_PROVINCE;
				$this->cond_unit = $this->province_id;		
				$sqlCondition.= " b.province_id in ($this->province_id) ";
				//$sql_count.= " b.province_id in ($this->province_id)";
			}else if($this->level == $this->LEVEL_CITY){
				//S市级		
				//echo "LEVEL_CITY";
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_CITY;
				$this->cond_unit = $this->city_id;
				$sqlCondition.= " b.city_id in ($this->city_id) ";
				//$sql_count.= " b.city_id in ($this->province_id)";
			}else if($this->level == $this->LEVEL_COUNTY){
				//县级			
				$this->cond_unit = $this->county_id;
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_COUNTY;
				$sqlCondition.= " b.county_id in ($this->county_id) ";
				//$sql_count.= " b.county_id in ($this->county_id)";
			}elseif($this->level == $this->LEVEL_BASE) {
				//站点			s
				$this->cond_unit = $this->base_id;
				$sqlCondition.= " b.base_id in ($this->base_id) ";
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_BASE;
				//$sql_count.= " b.base_id in ($this->base_id)";
			}else{
			 
				$this->cond_unit = "";
				$bases = MtsysBase::model()->searchByUserid();
				if($bases != null){
					$basesData = $bases->getData();
					$this->base_id = null;
					foreach($basesData as $base ){
						$this->base_id .=$base["BASE_ID"].' , ';
					}
					if($this->base_id != null) {
						$this->base_id[strlen($this->base_id)-2] =' ';
					}		
				}				
				if($this->base_id != null && $this->base_id != ""){
					$sqlCondition.= " b.base_id in ($this->base_id) ";
				}else {
					$sqlCondition.= " b.base_id in ( -1 ) ";
				}
			}
		
		if(isset($manufacturer))
		{
			if (strpos( $manufacturer , '|') !== false) {
				$this->group_option = $this->GROUP_BY_MANUFACTURER;
			}
		
			$this->manufacturer = str_replace("|", " , " , $manufacturer);
		}
		
		if(isset($size))
		{
			if (strpos( $size , '|') !== false) {
				$this->group_option = $this->GROUP_BY_SIZE;
			}
				
			$this->bt_type = str_replace("|", " , " , $size);
		}
		if(isset($str_time))
		{
			 
			$this->select_timer = $str_time;
			if (strpos( $str_time , '|') !== false) {
				$this->group_option = $this->GROUP_BY_TIME;
			}
			$this->time = str_replace("|", " , " , $str_time);
		}

		//$size = $_POST['select-size'];
		if(isset($_GET['status'])){
					$this->status = $_GET['status'];
			//to get the details data

			// to concat the details sql string
			$this->option_id =  $_GET['option_id'];
			if( $this->group_option == $this->GROUP_BY_NOTHING){ //super user and no option
				echo "super user no select";
							 
				if($user_unit_level == 0){
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}else if($user_unit_level == 4){
					$sql_details.= " b.base_id in ($user_unit_id)";
					$count_details.= " b.base_id in ($user_unit_id)";
				}
			}elseif($this->group_option > $this->GROUP_BY_TIME){ //单位多选或者分组
				//echo $this->level ;
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					//echo "test unit";
					$sql_details.= " b.province_id in ($this->province_id)";
					$count_details.= " b.province_id in ($this->province_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->city_id)";
					$count_details.= " b.city_id in ($this->city_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->county_id)";
					$count_details.= " b.county_id in ($this->county_id)";
				}elseif ($this->level == $this->LEVEL_BASE){
					//站点
					$sql_details.= " b.base_id in ($this->base_id)";
					$count_details.= " b.base_id in ($this->base_id)";
				}else{
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->manufacturer)";
					$count_details.= " and  dm.RECORD_ID  in ($this->manufacturer)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					$sql_details.= " and db.RECORD_ID in ($this->bt_type)";
					$count_details.= " and db.RECORD_ID in ($this->bt_type)";
				}
				if ($this->time != null && $this->time != "" )
				{
					$sql_details.= " and year(bt.SETUP_D) in ($this->option_id)";
					$count_details.= " and year(bt.SETUP_D) in ($this->option_id)";
				}					
			}elseif($this->group_option ==$this->GROUP_BY_SIZE){// 多选
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					//echo "test unit";
					$sql_details.= " b.province_id in ($this->province_id)";
					$count_details.= " b.province_id in ($this->province_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->city_id)";
					$count_details.= " b.city_id in ($this->city_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->county_id)";
					$count_details.= " b.county_id in ($this->county_id)";
				}elseif ($this->level == $this->LEVEL_BASE){
					//站点
					$sql_details.= " b.base_id in ($this->base_id)";
					$count_details.= " b.base_id in ($this->base_id)";
				}else{
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->manufacturer)";
					$count_details.= " and  dm.RECORD_ID  in ($this->manufacturer)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					$sql_details.= " and db.RECORD_ID in ($this->option_id)";
					$count_details.= " and db.RECORD_ID in ($this->option_id)";
				}
				if ($this->time != null && $this->time != "" )
				{
					$sql_details.= " and year(bt.SETUP_D) in ($this->time)";
					$count_details.= " and year(bt.SETUP_D) in ($this->time)";
				}
			}elseif($this->group_option ==$this->GROUP_BY_MANUFACTURER){
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					//echo "test unit";
					$sql_details.= " b.province_id in ($this->province_id)";
					$count_details.= " b.province_id in ($this->province_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->city_id)";
					$count_details.= " b.city_id in ($this->city_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->county_id)";
					$count_details.= " b.county_id in ($this->county_id)";
				}elseif ($this->level == $this->LEVEL_BASE){
					//站点
					$sql_details.= " b.base_id in ($this->base_id)";
					$count_details.= " b.base_id in ($this->base_id)";
				}else{
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->option_id)";
					$count_details.= " and  dm.RECORD_ID  in ($this->option_id)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					$sql_details.= " and db.RECORD_ID in ($this->bt_type)";
					$count_details.= " and db.RECORD_ID in ($this->bt_type)";
				}
				if ($this->time != null && $this->time != "" )
				{
					$sql_details.= " and year(bt.SETUP_D) in ($this->time)";
					$count_details.= " and year(bt.SETUP_D) in ($this->time)";
				}
			}else{
				/*
				$sql_details.= " b.base_id in ($this->option_id)";
				$count_details.= " b.base_id in ($this->option_id)"; */
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					$sql_details.= " b.province_id in ($this->option_id)";
					$count_details.= " b.province_id in ($this->option_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->option_id)";
					$count_details.= " b.city_id in ($this->option_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->option_id)";
					$count_details.= " b.county_id in ($this->option_id)";
				}elseif($this->level == $this->LEVEL_BASE) {
					//站点
					$sql_details.= " b.base_id in ($this->option_id)";
					$count_details.= " b.base_id in ($this->option_id)";
				}else{
					//站点
					if($this->option_id != null && $this->option_id == "none" ){
						 if($user_unit_level == 0){
							$sql_details.= " 1 ";
							$count_details.= " 1 ";
						}else if($user_unit_level == 4){
							$sql_details.= " b.base_id in ($user_unit_id)";
							$count_details.= " b.base_id in ($user_unit_id)";
						}
					}else{
						if($this->manufacturer != null && $this->manufacturer != ""){
							$sql_details.= " dm.RECORD_ID  in ($this->option_id)";
							$count_details.= " dm.RECORD_ID  in ($this->option_id)";
						}else if($this->bt_type != null && $this->bt_type != ""){
							$sql_details.= " db.RECORD_ID in ($this->option_id)";
							$count_details.= " db.RECORD_ID in ($this->option_id)";
						}else if ($this->time != null && $this->time != "" ){
							$sql_details.= "  year(bt.SETUP_D) in ($this->option_id)";
							$count_details.= "  year(bt.SETUP_D) in ($this->option_id)";
						} 						 
				    }
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->manufacturer)";
					$count_details.= " and  dm.RECORD_ID  in ($this->manufacturer)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					$sql_details.= " and db.RECORD_ID in ($this->bt_type)";
					$count_details.= " and db.RECORD_ID in ($this->bt_type)";
				}
				if ($this->time != null && $this->time != "" )
				{
					$sql_details.= " and year(bt.SETUP_D) in ($this->time)";
					$count_details.= " and year(bt.SETUP_D) in ($this->time)";
				}
			} 	
			 
			$sql_details.= " and bt.STATUS_N = $this->status";
			$count_details.= " and bt.STATUS_N = $this->status";
			
		}else{

			$sql_details.= " 0";
			$count_details.= " 0";
		}
		//var_dump($sql_details);

		
		$count=Yii::app()->db->createCommand($count_details)->queryScalar();
		$dataProvider_details = new CSqlDataProvider($sql_details,
				array(
						'totalItemCount'=>$count,
						'pagination'=>array(
						'pageSize'=>10,
						),
				));
		
		//var_dump($dataProvider);
		$this->render('color',array(				
				//'option_id'=>$this->option_id,				
				'dataProvider_details'=>$dataProvider_details,
		));
	
	}
	
	
	/** by hyq
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */

	/** by hyq
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionImportdata()
	{
 
		$sqlCondition ='';
		$no_unit = 0;
		$str_unit ;
		$manufacturer;
		$size;
		$str_time;
		$status;
		$sql_details = $this->sql_details_table;
		$count_details = $this->count_details_table;
		//获取用户所属单位的级别
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$user_unit_level = $user->unit_level;
		$user_unit_id = $user->unit_id;
 
		if(isset($_GET['redirect']))
		{
			$str_unit =$_GET['select_unit'];
			$manufacturer = $_GET['select_maker'];
			$size = $_GET['select_size'];
			$str_time = $_GET['select_timer'];
			//echo 'redirect';
		
		}else {
			$str_unit =$_POST['select-unit'];
			$manufacturer = $_POST['select-maker'];
			$size = $_POST['select-size'];
			$str_time = $_POST['select-timer'];
		}
		$this->select_unit = $str_unit;
		$this->select_maker = $manufacturer;
		$this->select_size = $size;
		$this->select_timer = $str_time;
		////echo 'unit'.$_POST['select-unit'];
		//$str_unit =$_POST['select-unit'];
		//$str_unit ="14,3,1|14,4,3";
		////echo 	$str_unit."str_unit".strlen($str_unit);
			if(isset($str_unit)){
			//echo "$str_unit=".$str_unit;
			$this->select_unit = $str_unit;
			if ($str_unit == null || strlen($str_unit) ==0  ) {
				////echo 	$this->group_option."level";
				$no_unit = 1;
				if($user_unit_level >= $this->LEVEL_PROVINCE ){
					if($user_unit_level == $this->LEVEL_PROVINCE){
						$this->level =$this->LEVEL_PROVINCE; 
						$this->province_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_PROVINCE;
					}elseif($user_unit_level == $this->LEVEL_CITY){
						$this->level =$this->LEVEL_CITY;
						$this->city_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_CITY;
					}elseif($user_unit_level == $this->LEVEL_COUNTY){
						$this->level =$this->LEVEL_COUNTY;
						$this->county_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_COUNTY;
					}elseif($user_unit_level == $this->LEVEL_BASE){
						/*
						$this->level =$this->LEVEL_BASE;
						$this->base_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_BASE;
							*/
					}
				}
			}else{

			$this->unit_visible_tag = true;	
			//split and split the unit string
			$units = explode("|", $str_unit);
			$numbers = count($units); //count 
			
			$all_units = explode(",", $units[0]);
			$this->level = count($all_units); //count the level
 
			$this->province_id = "";
			$this->city_id = "";
			$this->county_id = "";	
			$this->base_id = "";
			
			//parse the units_str
			/*foreach ($units as $unit){
				//echo $unit;
				$all_units = explode(",", $unit);
				$this->level = count($all_units); //count the level
				if($this->level == $this->LEVEL_PROVINCE){ //province
					$this->province_id .= ','.$all_units[0];
				}elseif($this->level == $this->LEVEL_CITY){ //city
					$this->province_id .= ','.$all_units[0];
					$this->city_id .= ','.$all_units[1];
				}elseif($this->level == $this->LEVEL_COUNTY){ //county
					$this->province_id .= ','.$all_units[0];
					$this->city_id .= ','.$all_units[1];
					$this->county_id .= ','.$all_units[2];
				}else if($this->level == $this->LEVEL_BASE){ //base
					$this->province_id .= ','.$all_units[0];	
					$this->city_id .= ','.$all_units[1];
					$this->county_id .= ','.$all_units[2];
					$this->base_id .= ','.$all_units[3];
				}				
			}
			
			$this->province_id = substr($this->province_id, 1, strlen($this->province_id)-1); 
			$this->city_id = substr($this->city_id, 1, strlen($this->city_id)-1); 
			$this->county_id = substr($this->county_id, 1, strlen($this->county_id) -1 ); 
			$this->base_id = substr($this->base_id, 1, strlen($this->base_id)-1); */
			
			$flag_for_choise_level_upper_unit_level = 0;
			//parse the units_str
			foreach ($units as $unit){
				//echo $unit;
				$all_units = explode(",", $unit);
				$this->level = count($all_units); //count the level
					
				if($this->level < intval($user_unit_level)){
					//如果选择的级别高于用户级别，使用用户级别
					$flag_for_choise_level_upper_unit_level = 1;
					break;
				}else{
					//如果用户选择级别低于用户级别，使用选择数据
					$flag_for_choise_level_upper_unit_level = 0;
				}
					
				//else
				{
					if($this->level == $this->LEVEL_PROVINCE){ //province
						$this->province_id .= ','.$all_units[0];
					}elseif($this->level == $this->LEVEL_CITY){ //city
						$this->province_id .= ','.$all_units[0];
						$this->city_id .= ','.$all_units[1];
					}elseif($this->level == $this->LEVEL_COUNTY){ //county
						$this->province_id .= ','.$all_units[0];
						$this->city_id .= ','.$all_units[1];
						$this->county_id .= ','.$all_units[2];
					}else if($this->level == $this->LEVEL_BASE){ //base
						$this->province_id .= ','.$all_units[0];
						$this->city_id .= ','.$all_units[1];
						$this->county_id .= ','.$all_units[2];
						$this->base_id .= ','.$all_units[3];
					}
				}
			}
			
			if($flag_for_choise_level_upper_unit_level == 0){
				//如果用户选择级别低于用户级别，使用选择数据
				$this->province_id = substr($this->province_id, 1, strlen($this->province_id)-1);
				$this->city_id = substr($this->city_id, 1, strlen($this->city_id)-1);
				$this->county_id = substr($this->county_id, 1, strlen($this->county_id) -1 );
				$this->base_id = substr($this->base_id, 1, strlen($this->base_id)-1);
			}else{
				//如果选择的级别高于用户级别，使用用户级别
				if($user_unit_level >= $this->LEVEL_PROVINCE ){
					if($user_unit_level == $this->LEVEL_PROVINCE){
						$this->level =$this->LEVEL_PROVINCE;
						$this->province_id  = $user_unit_id ;
						//echo "group_option".$this->group_option;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_PROVINCE;
					}elseif($user_unit_level == $this->LEVEL_CITY){
						$this->level =$this->LEVEL_CITY;
						$this->city_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_CITY;
					}elseif($user_unit_level == $this->LEVEL_COUNTY){
						$this->level =$this->LEVEL_COUNTY;
						$this->county_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_COUNTY;
					}elseif($user_unit_level == $this->LEVEL_BASE){
						$this->level =$this->LEVEL_BASE;
						$this->base_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_BASE;
					}
				}
			}
			
			if($this->level == $this->LEVEL_PROVINCE){
				//省级比较
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_PROVINCE;
			}else if($this->level == $this->LEVEL_CITY){
				//S市级
				//echo "LEVEL_CITY";
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_CITY;
			}else if($this->level == $this->LEVEL_COUNTY){
				//县级
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_COUNTY;
			}elseif($this->level == $this->LEVEL_BASE) {
				//站点			s
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_BASE;
			}
			}
		}
		
		
		if(isset($manufacturer))
		{
			//echo 'manufacturer'.$_POST['select-manufacturer'];
			//$manufacturer = $_POST['select-manufacturer'];
		
			if (strpos( $manufacturer , '|') !== false) {
				$this->group_option = 1;
			}
		
			$this->manufacturer = str_replace("|", " , " , $manufacturer);
		}
		
		if(isset($size))
		{
			//echo 'size'.$_POST['select-size'];
				
			//$size = $_POST['select-size'];
				
			if (strpos( $size , '|') !== false) {
				$this->group_option = 2;
			}
				
			$this->bt_type = str_replace("|", " , " , $size);
		}
		if(isset($str_time))
		{
			 
			$this->select_timer = $str_time;
			if (strpos( $str_time , '|') !== false) {
				$this->group_option = $this->GROUP_BY_TIME;
			}
			$this->time = str_replace("|", " , " , $str_time);
		}
			if($this->level == 1){
				//省级比较			
				$sqlCondition.= " b.province_id in ($this->province_id) ";
				$sql_details.= " b.province_id in ($this->province_id)";
				$count_details.= " b.province_id in ($this->province_id)";
				//$sql_count.= " b.province_id in ($this->province_id)";
			}else if($this->level == 2){
				//S市级			
				$sqlCondition.= " b.city_id in ($this->city_id) ";
				$sql_details.= " b.city_id in ($this->city_id) ";
				$count_details.= " b.city_id in ($this->city_id) ";
				//$sql_count.= " b.city_id in ($this->province_id)";
			}else if($this->level == 3){
				//县级			
				$sqlCondition.= " b.county_id in ($this->county_id) ";
				$sql_details.= " b.county_id in ($this->county_id) ";
				$count_details.= " b.county_id in ($this->county_id) ";
			}elseif($this->level == 4) {
				//站点
				$sqlCondition.= " b.base_id in ($this->base_id) ";
				$sql_details.= " b.base_id in ($this->base_id) ";
				$count_details.= " b.base_id in ($this->base_id) ";
			}else{
				$bases = MtsysBase::model()->searchByUserid();
				if($bases != null){
					$basesData = $bases->getData();
					$this->base_id = null;
					foreach($basesData as $base ){
						$this->base_id .=$base["BASE_ID"].' , ';
					}
					if($this->base_id != null) {
						$this->base_id[strlen($this->base_id)-2] =' ';
					}		
				}	
				if($this->base_id != null && $this->base_id != ""){
					$sqlCondition.= " b.base_id in ($this->base_id) ";
					$sql_details.= " b.base_id in ($this->base_id) ";
					$count_details.=" b.base_id in ($this->base_id) "; 
				}else {
					$sqlCondition.= " b.base_id in ( -1 ) ";
					$sql_details.= " b.base_id in ( -1) ";
					$count_details.=" b.base_id in ( -1) ";
				}
			}
			
			if($this->manufacturer != null && $this->manufacturer != ""){		
				$sqlCondition.= " and dm.RECORD_ID  in ($this->manufacturer) ";
				$sql_details.= " and dm.RECORD_ID  in ($this->manufacturer) ";
				$count_details.= " and dm.RECORD_ID  in ($this->manufacturer) ";
				//$sql_count.= "  and dm.RECORD_ID  in ($this->manufacturer)";
			}
			if($this->bt_type != null && $this->bt_type != ""){
				$sqlCondition.= " and db.RECORD_ID in ($this->bt_type) ";
				$sql_details.= " and db.RECORD_ID in ($this->bt_type) ";
				$count_details.= " and db.RECORD_ID in ($this->bt_type) ";
				//$sql_count.= " and db.RECORD_ID in ($this->bt_type)";
			}
			if ($this->time != null && $this->time != "") {
				$sqlCondition.= " and year(bt.SETUP_D ) in ($this->time) ";
				$sql_details.= " and year(bt.SETUP_D ) in ($this->time) ";
				$count_details.= " and year(bt.SETUP_D ) in ($this->time) ";
			}	
			//var_dump($this->group_option);
			if($this->group_option == $this->GROUP_BY_MANUFACTURER){//制造商多选
				//echo "group_option = GROUP_BY_MANUFACTURER";
				$this->maker_visible_tag = true;
				//echo "unit_visible_tag =".$this->unit_visible_tag;
				//echo "maker_visible_tag =".$this->maker_visible_tag;
				//$sql.= " GROUP BY db.BATTERYTYPE_V )  g_table ,(SELECT @row :=0)r";
				$sqlGroup= " GROUP BY bt.MANCODE_V ) g_table ";
					
				$greenSql = sprintf($this->state_sql, 'green_n' , 'dm.RECORD_ID', $this->GREEN );
				$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'dm.RECORD_ID',  $this->YELLOW);
				$redSql = sprintf($this->state_sql, 'red_n' , 'dm.RECORD_ID', $this->RED );
			
				$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cg', 'cg.RECORD_ID',   'bt.MANCODE_V');
				$greenSql=$greenSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cy', 'cy.RECORD_ID',   'bt.MANCODE_V');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cr', 'cr.RECORD_ID',   'bt.MANCODE_V');
				$redSql=$redSql.$sqlCondition.$group_condition;
			
			}elseif($this->group_option == $this->GROUP_BY_SIZE){//电池规格多选
					
				$this->guige_visible_tag = true;
				$sqlGroup = " GROUP BY bt.BATTERYTYPE_V ) g_table ";
				$greenSql = sprintf($this->state_sql, 'green_n' , 'db.RECORD_ID', $this->GREEN );
				$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'db.RECORD_ID',  $this->YELLOW);
				$redSql = sprintf($this->state_sql, 'red_n' , 'db.RECORD_ID', $this->RED );
			
				$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cg', 'cg.RECORD_ID',   'bt.BATTERYTYPE_V');
				$greenSql=$greenSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cy', 'cy.RECORD_ID',   'bt.BATTERYTYPE_V');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cr', 'cr.RECORD_ID',   'bt.BATTERYTYPE_V');
				$redSql=$redSql.$sqlCondition.$group_condition;
			}elseif($this->group_option == $this->GROUP_BY_TIME){//电池规格多选
				$this->time_visible_tag = true;
							$sqlGroup = " GROUP BY year(bt.SETUP_D) ) g_table ";
				$greenSql = sprintf($this->state_sql, 'green_n' , ' year(bt.SETUP_D) SETUP_D', $this->GREEN );
				$yellowSql = sprintf($this->state_sql, 'yellow_n' , ' year(bt.SETUP_D) SETUP_D',  $this->YELLOW);
				$redSql = sprintf($this->state_sql, 'red_n' , ' year(bt.SETUP_D) SETUP_D', $this->RED );
		
				$group_condition  = sprintf($this->condition_sql, ' year(bt.SETUP_D) ','cg', ' cg.SETUP_D',   ' year(bt.SETUP_D) ');
				$greenSql=$greenSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, ' year(bt.SETUP_D) ','cy', ' cy.SETUP_D',   ' year(bt.SETUP_D) ');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'year(bt.SETUP_D) ','cr', ' cr.SETUP_D ',   ' year(bt.SETUP_D) ');
				$redSql=$redSql.$sqlCondition.$group_condition;
	
			}elseif( ($user_unit_level < $this->LEVEL_PROVINCE ||$user_unit_level == $this->LEVEL_BASE) && $no_unit){ //super user and no option
				//select all data

				$this->group_option = $this->GROUP_BY_NOTHING;
				$userid = Yii::app()->user->id;
				$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
				$unit_level = $user->unit_level;
				$unit_id = $user->unit_id;
				//三项都不显示
				$this->maker_visible_tag = false;
				$this->guige_visible_tag = false;
				$this->unit_visible_tag = false;
				
							//bug 4.19 这里只有两种情况，基站管理员和超级管理员
				if($user->unit_level == 0){
				$state ="SELECT count(1) From  bt_batterys where status_n = %d   " ;

				$sql_fmt = "SELECT @row := @row +1 AS id, count(1) , (%s) as green_n, (%s) as yellow_n, (%s) as red_n
						From bt_batterys, (SELECT @row :=0)r ";
				}else if($user->unit_level == 4){
					$state ="SELECT count(1) From  bt_batterys where status_n = %d  and  base_n in ( $unit_id )" ;
					$sql_fmt = "SELECT @row := @row +1 AS id, count(1) , (%s) as green_n, (%s) as yellow_n, (%s) as red_n
						From bt_batterys  , (SELECT @row :=0)r where  base_n in ( $unit_id )";					
				}
				$greenSql = sprintf($state, 0);
				$yellowSql = sprintf($state, 1);
				$redSql = sprintf($state, 2);
				
				$sql = sprintf($sql_fmt, $greenSql, $yellowSql,$redSql);
				$sql_count = "select count(1) from bt_batterys";
			}elseif($this->group_option == $this->GROUP_BY_PROVINCE){//省级比较
				//echo "group_option = GROUP_BY_PROVINCE";
					$this->unit_visible_tag = true;	
					$sqlGroup= " GROUP BY b.province_id ) g_table ";
					$greenSql = sprintf($this->state_sql, 'green_n' , 'b.province_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.province_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.province_id', $this->RED );
				
					$group_condition  = sprintf($this->condition_sql, 'b.province_id','cg', 'cg.province_id',   'b.province_id');
					$greenSql=$greenSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.province_id','cy', 'cy.province_id',   'b.province_id');
					$yellowSql=$yellowSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.province_id','cr', 'cr.province_id',   'b.province_id');
					$redSql=$redSql.$sqlCondition.$group_condition;
				
			}else if($this->group_option == $this->GROUP_BY_CITY){//S市级	
				//echo "group_option = GROUP_BY_CITY";
					$this->unit_visible_tag = true;
					//$sql.= " GROUP BY b.city_id ) g_table ,(SELECT @row :=0)r";
					$sqlGroup= " GROUP BY b.city_id ) g_table ";
						
					$greenSql = sprintf($this->state_sql, 'green_n' , 'b.city_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.city_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.city_id', $this->RED );
				
					$group_condition  = sprintf($this->condition_sql, 'b.city_id','cg', 'cg.city_id', 'b.city_id');
					$greenSql=$greenSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.city_id','cy', 'cy.city_id', 'b.city_id');
					$yellowSql=$yellowSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.city_id','cr', 'cr.city_id', 'b.city_id');
					$redSql=$redSql.$sqlCondition.$group_condition;
				
			}else if($this->group_option == $this->GROUP_BY_COUNTY){//县级
					$this->unit_visible_tag = true;
				 
					$sqlGroup= " GROUP BY b.county_id ) g_table ";
					$greenSql = sprintf($this->state_sql, 'green_n' , 'b.county_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.county_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.county_id', $this->RED );
				
					$group_condition  = sprintf($this->condition_sql, 'b.county_id','cg', 'cg.county_id', 'b.county_id');
					$greenSql=$greenSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.county_id','cy', 'cy.county_id', 'b.county_id');
					$yellowSql=$yellowSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.county_id','cr', 'cr.county_id', 'b.county_id');
					$redSql=$redSql.$sqlCondition.$group_condition;
				
			}else if($this->group_option == $this->GROUP_BY_BASE){//站点
				
					$this->unit_visible_tag = true;
					$sqlGroup= " GROUP BY b.base_id ) g_table ";
				
					$greenSql = sprintf($this->state_sql, 'green_n' , 'b.base_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.base_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.base_id', $this->RED );
						
					$group_condition  = sprintf($this->condition_sql, 'b.base_id','cg', 'cg.base_id', 'b.base_id');
					$greenSql=$greenSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.base_id','cy', 'cy.base_id', 'b.base_id');
					$yellowSql=$yellowSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.base_id','cr', 'cr.base_id', 'b.base_id');
					$redSql=$redSql.$sqlCondition.$group_condition;				 
			} 	else{	
				//goto select the condition by hyq
						$this->actionAdmin();
			}
			
			//concat the sql 4 total
			if( $this->group_option != $this->GROUP_BY_NOTHING){ //super user and no option			
				$sql  = $this->sql_fields.$this->tables." LEFT join ".$greenSql." LEFT join ".$yellowSql." LEFT join ".$redSql." where ".$sqlCondition.$sqlGroup.",(SELECT @row :=0)r";
				$sql_count  = $this->sql_count.$this->tables." where ".$sqlCondition.$sqlGroup;
			}
		//var_dump($sql) ;
 	//$dataProvider = new CSqlDataProvider($sql);
	//$dataProvider_details = new CSqlDataProvider($sql_details);
	
	$count_record=Yii::app()->db->createCommand($sql_count)->queryScalar();
	////////echo $count_record."aaa";
 
	$dataProvider = new CSqlDataProvider($sql,
			array(
					'totalItemCount'=>$count_record,
					'pagination'=>array(
							'pageSize'=>$count_record,
					),
			));
	
	$count_record=Yii::app()->db->createCommand($count_details)->queryScalar();
	//////echo $count_record."aaa";
	$dataProvider_details = new CSqlDataProvider($sql_details,
			array(
					'totalItemCount'=>$count_record,
					'pagination'=>array(
					'pageSize'=>$count_record,
					),
			));
	
	/////////////////////////////////////////////////////////////////////////////////////
	// set the excel file with data
	Yii::import('application.modules.vendors.PHPExcel',true);
	
	$objectPHPExcel = new PHPExcel();
	$objectPHPExcel->setActiveSheetIndex(0);
	
	$objectPHPExcel->getActiveSheet()->setCellValue('A1', '流水号');
	$objectPHPExcel->getActiveSheet()->setCellValue('B1', '统计单位');
	/*
	if($this->unit_visible_tag)
		$objectPHPExcel->getActiveSheet()->setCellValue('B1', '统计单位');
	if($this->maker_visible_tag)
		$objectPHPExcel->getActiveSheet()->setCellValue('B2', '制造商');
	if($this->guige_visible_tag)
		$objectPHPExcel->getActiveSheet()->setCellValue('B3', '电池规格');
	*/
	$objectPHPExcel->getActiveSheet()->setCellValue('C1', '合计');
	$objectPHPExcel->getActiveSheet()->setCellValue('D1', '绿色正常');
	$objectPHPExcel->getActiveSheet()->setCellValue('E1', '黄色警告');
	$objectPHPExcel->getActiveSheet()->setCellValue('F1', '红色警告');
	
	//$dataprovider->setPagination(false);
	$data = $dataProvider->getData();
	$count = $dataProvider->getTotalItemCount();
	
	$n = 1;
	foreach ( $data  as $k=>$value ){
		
	$objectPHPExcel->getActiveSheet()->setCellValue('A'.($n+1) ,$data[$k]['id']);
	if($this->level == 1 ){
		$objectPHPExcel->getActiveSheet()->setCellValue('b'.($n+1) ,$data[$k]['province_name']);
	}elseif($this->level == 2){
		$objectPHPExcel->getActiveSheet()->setCellValue('b'.($n+1) ,$data[$k]['province_name'].$data[$k]['city_name']);
	}elseif($this->level == 3){
		$objectPHPExcel->getActiveSheet()->setCellValue('b'.($n+1) ,$data[$k]['province_name'].$data[$k]['city_name'].$data[$k]['county_name']);
	}elseif($this->level == 4){
		$objectPHPExcel->getActiveSheet()->setCellValue('b'.($n+1) ,$data[$k]['province_name'].$data[$k]['city_name'].$data[$k]['county_name'].$data[$k]['base_name']);
	}else{
		$objectPHPExcel->getActiveSheet()->setCellValue('b'.($n+1) ,"所有单位");
	}
	//$objectPHPExcel->getActiveSheet()->setCellValue('B'.($n+1) ,$data[$k]['province_name'].$data[$k]['city_name'].$data[$k]['county_name'].$data[$k]['base_name']);
	$objectPHPExcel->getActiveSheet()->setCellValue('C'.($n+1) ,$data[$k]['green_n']+$data[$k]['yellow_n']+$data[$k]['red_n']);
		$objectPHPExcel->getActiveSheet()->setCellValue('D'.($n+1) ,$data[$k]['green_n']);
		$objectPHPExcel->getActiveSheet()->setCellValue('E'.($n+1) ,$data[$k]['yellow_n']);
		$objectPHPExcel->getActiveSheet()->setCellValue('F'.($n+1) ,$data[$k]['red_n']);
		/*
		$objectPHPExcel->getActiveSheet()->setCellValue('A'.($n+1) ,$product->base_id);
		//$objectPHPExcel->getActiveSheet()->setCellValue('C'.($n+1) ,$product->base_id);
		$objectPHPExcel->getActiveSheet()->setCellValue('B'.($n+1) ,$product->name);
				$objectPHPExcel->getActiveSheet()->setCellValue('C'.($n+1) ,$product->green);
		$objectPHPExcel->getActiveSheet()->setCellValue('D'.($n+1) ,$product->yellow);
		$objectPHPExcel->getActiveSheet()->setCellValue('E'.($n+1) ,$product->red);
		*/
		$n+=1;
		}
		$objectPHPExcel->getActiveSheet()->setTitle("分类统计");

	
		////////////////////////////////////////details/////////////////////////////////////////////
		$objectPHPExcel->createSheet();
		$objectPHPExcel->setActiveSheetIndex(1);
	
		$objectPHPExcel->getActiveSheet()->setCellValue('A1', '流水号');
		$objectPHPExcel->getActiveSheet()->setCellValue('B1', '站点名称');
				$objectPHPExcel->getActiveSheet()->setCellValue('C1', '组别名称');
				//$objectPHPExcel->getActiveSheet()->setCellValue('D1', '序列号');
				$objectPHPExcel->getActiveSheet()->setCellValue('D1', '制造商');
				$objectPHPExcel->getActiveSheet()->setCellValue('E1', '电池类型');
				//$objectPHPExcel->getActiveSheet()->setCellValue('G1', '电池年代');
				$objectPHPExcel->getActiveSheet()->setCellValue('F1', '安装日期');
				$objectPHPExcel->getActiveSheet()->setCellValue('G1', '状态');
	
				//$dataprovider->setPagination(false);
				$data = $dataProvider_details->getData();
		$count = $dataProvider_details->getTotalItemCount();
	
		$n = 1;
			foreach ( $data  as $k=>$value ){
	
			$objectPHPExcel->getActiveSheet()->setCellValue('A'.($n+1) ,$data[$k]['id']);
			//$objectPHPExcel->getActiveSheet()->setCellValue('d'.($n+1) ,$data[$k]['base_id']);
			//$objectPHPExcel->getActiveSheet()->setCellValue('C'.($n+1) ,$data[$k]['base_id']);
			 

			$objectPHPExcel->getActiveSheet()->setCellValue('b'.($n+1) ,$data[$k]['province_name'].$data[$k]['city_name'].$data[$k]['county_name'].$data[$k]['base_name']);
			$objectPHPExcel->getActiveSheet()->setCellValue('c'.($n+1) ,$data[$k]['GROUPNAME_V']);
			//$objectPHPExcel->getActiveSheet()->setCellValue('d'.($n+1) ,$data[$k]['bt_id']);
			$objectPHPExcel->getActiveSheet()->setCellValue('D'.($n+1) ,$data[$k]['MANNAME_V']);
			$objectPHPExcel->getActiveSheet()->setCellValue('E'.($n+1) ,$data[$k]['SORTNAME_V']);
			//$objectPHPExcel->getActiveSheet()->setCellValue('g'.($n+1) ,$data[$k]['bt_date']);
			$objectPHPExcel->getActiveSheet()->setCellValue('F'.($n+1) ,$data[$k]['bt_date']);
			$objectPHPExcel->getActiveSheet()->setCellValue('G'.($n+1) ,$data[$k]['STATUS_N'] == 0 ? "绿色" :($data[$k]['STATUS_N'] == 1 ? "黄色" : "红色"));
			//$objectPHPExcel->getActiveSheet()->setCellValue('F'.($n+1) ,$data[$k]['red']);
			$n+=1;
			}
			$objectPHPExcel->getActiveSheet()->setTitle("详细统计");
	
			$objWriter = new PHPExcel_Writer_Excel5($objectPHPExcel);
			header("Pragma: public");
			header("Expires: 0");
	
	
			ob_end_clean();
			ob_start();
			header("Pragma: public");
			header("Expires: 0");
			//header('Content-Type: application/vnd.ms-excel;charset=utf8');
			header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
			header("Content-Type:application/force-download");
			header("Content-Type:application/vnd.ms-execl");
			header("Content-Type:application/octet-stream");
			//header("Content-Type:application/download");
			//header('Content-Type : application/vnd.ms-excel');
		header('Content-Disposition:attachment;filename="'.'分类统计'.'.xls"');
		$objWriter= PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5');
		$objWriter->save('php://output');
	

	}	
	
	
	/** by hyq
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 * 1.首先看有没有多选  
有多选 如果是单位，就直接按单位分组  
		非单位		就列出单位的条数
无多选 如果非管理员 按所属单位分组
		如果是管理员 选了什么就按什么分组
					 如果没选什么就统计所有
	 */
	public function actionDetails()
	{

		$dataProvider_details ;
		$dataProvider;
		$group_option = -1; // -1 : no muti; 0 : unit ; 1 :manufacturer; 2 :size; 
		$sql_count;
		$sql;
		$greenSql;
		$yellowSql;
		$redSql;
		$sqlCondition = '';
		$sqlGroup = '';
		

		$no_unit = 0;
		$str_unit ;
		$manufacturer;
		$size;
		$str_time;
		$status;
		
		$sql_details = $this->sql_details_table;
		$count_details = $this->count_details_table;
		//获取用户所属单位的级别
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$user_unit_level = $user->unit_level;
		$user_unit_id = $user->unit_id;
		
		//echo "user_unit_level=".$user_unit_level;
		if(isset($_GET['redirect']))
		{
			//echo "redirect";
			$str_unit =$_GET['select_unit'];
			$manufacturer = $_GET['select_maker'];
			$size = $_GET['select_size'];
			$str_time = $_GET['select_timer'];
			//echo 'redirect';
		}else {
			//echo "_POST";
			if(isset($_POST['select-unit']))
				$str_unit =$_POST['select-unit'];
			if(isset($_POST['select-maker']))
				$manufacturer = $_POST['select-maker'];
			if(isset($_POST['select-size']))
				$size = $_POST['select-size'];
			if(isset($_POST['select-timer']))
				$str_time = $_POST['select-timer'];
		}
		if(isset($manufacturer))
		{
			//echo "manufacturer=".$manufacturer;
			$this->select_maker = $manufacturer;
			//echo 'manufacturer'.$_POST['select-manufacturer'];
			//$manufacturer = $_POST['select-manufacturer'];
			//echo 	$manufacturer;
			if (strpos( $manufacturer , '|') !== false) {
				$this->group_option = $this->GROUP_BY_MANUFACTURER;
			}
			$this->manufacturer = str_replace("|", " , " , $manufacturer);
		}
		
		if(isset($size))
		{
			//echo "size=".$size;
			$this->select_size = $size;
			if (strpos( $size , '|') !== false) {
				$this->group_option = $this->GROUP_BY_SIZE;
			}
			$this->bt_type = str_replace("|", " , " , $size);
		}
		
		if(isset($str_time))
		{
			 
			$this->select_timer = $str_time;
			if (strpos( $str_time , '|') !== false) {
				$this->group_option = $this->GROUP_BY_TIME;
			}
			$this->time = str_replace("|", " , " , $str_time);
		}
		
		if(isset($str_unit)){
			//echo "$str_unit=".$str_unit;
			$this->select_unit = $str_unit;
			if ($str_unit == null || strlen($str_unit) ==0  ) {
				//echo 	$this->group_option."level";
				$no_unit = 1;
				if($user_unit_level >= $this->LEVEL_PROVINCE ){
					if($user_unit_level == $this->LEVEL_PROVINCE){
						$this->level =$this->LEVEL_PROVINCE; 
						$this->province_id  = $user_unit_id ;
						//echo "group_option".$this->group_option;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_PROVINCE;
					}elseif($user_unit_level == $this->LEVEL_CITY){
						$this->level =$this->LEVEL_CITY;
						$this->city_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_CITY;
					}elseif($user_unit_level == $this->LEVEL_COUNTY){
						$this->level =$this->LEVEL_COUNTY;
						$this->county_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_COUNTY;
					}elseif($user_unit_level == $this->LEVEL_BASE){
 
						/*$this->level =$this->LEVEL_BASE;
						$this->base_id  = $user_unit_id ;
						
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_BASE;
						*/
					}
				}
			}else{
			
			$this->unit_visible_tag = true;	
			//split and split the unit string
			$units = explode("|", $str_unit);
			$numbers = count($units); //count 
			
			$all_units = explode(",", $units[0]);
			$this->level = count($all_units); //count the level
 
			$this->province_id = "";
			$this->city_id = "";
			$this->county_id = "";	
			$this->base_id = "";
			
			//parse the units_str
			$flag_for_choise_level_upper_unit_level = 0;
				//parse the units_str
				foreach ($units as $unit){
					//echo $unit;
					$all_units = explode(",", $unit);
					$this->level = count($all_units); //count the level					
					
					if($this->level < intval($user_unit_level)){
						//如果选择的级别高于用户级别，使用用户级别
						$flag_for_choise_level_upper_unit_level = 1;
						break;
					}else{
						//如果用户选择级别低于用户级别，使用选择数据
						$flag_for_choise_level_upper_unit_level = 0;
					}
				
					//else
					{					
						if($this->level == $this->LEVEL_PROVINCE){ //province
							$this->province_id .= ','.$all_units[0];
						}elseif($this->level == $this->LEVEL_CITY){ //city
							$this->province_id .= ','.$all_units[0];
							$this->city_id .= ','.$all_units[1];
						}elseif($this->level == $this->LEVEL_COUNTY){ //county
							$this->province_id .= ','.$all_units[0];
							$this->city_id .= ','.$all_units[1];
							$this->county_id .= ','.$all_units[2];
						}else if($this->level == $this->LEVEL_BASE){ //base
							$this->province_id .= ','.$all_units[0];
							$this->city_id .= ','.$all_units[1];
							$this->county_id .= ','.$all_units[2];
							$this->base_id .= ','.$all_units[3];
						}
					}					
				}
				
				if($flag_for_choise_level_upper_unit_level == 0){
					//如果用户选择级别低于用户级别，使用选择数据
					$this->province_id = substr($this->province_id, 1, strlen($this->province_id)-1);
					$this->city_id = substr($this->city_id, 1, strlen($this->city_id)-1);
					$this->county_id = substr($this->county_id, 1, strlen($this->county_id) -1 );
					$this->base_id = substr($this->base_id, 1, strlen($this->base_id)-1);
				}else{
					//如果选择的级别高于用户级别，使用用户级别
					if($user_unit_level >= $this->LEVEL_PROVINCE ){
						if($user_unit_level == $this->LEVEL_PROVINCE){
							$this->level =$this->LEVEL_PROVINCE;
							$this->province_id  = $user_unit_id ;
							//echo "group_option".$this->group_option;
							if($this->group_option < 0) //
								$this->group_option = $this->GROUP_BY_PROVINCE;
						}elseif($user_unit_level == $this->LEVEL_CITY){
							$this->level =$this->LEVEL_CITY;
							$this->city_id  = $user_unit_id ;
							if($this->group_option < 0) //
								$this->group_option = $this->GROUP_BY_CITY;
						}elseif($user_unit_level == $this->LEVEL_COUNTY){
							$this->level =$this->LEVEL_COUNTY;
							$this->county_id  = $user_unit_id ;
							if($this->group_option < 0) //
								$this->group_option = $this->GROUP_BY_COUNTY;
						}elseif($user_unit_level == $this->LEVEL_BASE){
							 
							$this->level =$this->LEVEL_BASE;
							$this->base_id  = $user_unit_id ;
							if($this->group_option < 0) //
								$this->group_option = $this->GROUP_BY_BASE;
						}
					}
				}
 
			}
		}
	
	 
			if($this->level == $this->LEVEL_PROVINCE){
				//省级比较		
				//echo "LEVEL_PROVINCE";	
				if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_PROVINCE;
				$this->cond_unit = $this->province_id;		
				$sqlCondition.= " b.province_id in ($this->province_id) ";
				//$sql_count.= " b.province_id in ($this->province_id)";
			}else if($this->level == $this->LEVEL_CITY){
				//S市级		
				//echo "LEVEL_CITY";
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_CITY;
				$this->cond_unit = $this->city_id;
				$sqlCondition.= " b.city_id in ($this->city_id) ";
				//$sql_count.= " b.city_id in ($this->province_id)";
			}else if($this->level == $this->LEVEL_COUNTY){
				//县级			
				$this->cond_unit = $this->county_id;
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_COUNTY;
				$sqlCondition.= " b.county_id in ($this->county_id) ";
				//$sql_count.= " b.county_id in ($this->county_id)";
			}elseif($this->level == $this->LEVEL_BASE) {
				//站点			s
				$this->cond_unit = $this->base_id;
				$sqlCondition.= " b.base_id in ($this->base_id) ";
				if($this->group_option < 0) //
					$this->group_option = $this->GROUP_BY_BASE;
				//$sql_count.= " b.base_id in ($this->base_id)";
			}else{
				
				$this->cond_unit = "";
				$bases = MtsysBase::model()->searchByUserid();
				if($bases != null){
					$basesData = $bases->getData();
					$this->base_id = null;
					foreach($basesData as $base ){
						$this->base_id .=$base["BASE_ID"].' , ';
					}
					if($this->base_id != null) {
						$this->base_id[strlen($this->base_id)-2] =' ';
					}		
				}				
				if($this->base_id != null && $this->base_id != ""){
					$sqlCondition.= " b.base_id in ($this->base_id) ";
				}else {
					$sqlCondition.= " b.base_id in ( -1 ) ";
				}
			}
			
			if($this->manufacturer != null && $this->manufacturer != ""){		
				$sqlCondition.= " and dm.RECORD_ID  in ($this->manufacturer) ";
				$this->maker_visible_tag = true;
			}
			if($this->bt_type != null && $this->bt_type != ""){
				$this->guige_visible_tag = true;
				$sqlCondition.= " and db.RECORD_ID in ($this->bt_type) ";
			}

			if ($this->time != null && $this->time != "") {
				$this->time_visible_tag = true;				 
				//SELECT year (bt.SETUP_D) FROM `bt_batterys` bt where  2012 =  year( bt.SETUP_D) or  2013  = year( bt.SETUP_D)   GROUP BY year (bt.SETUP_D)
				$sqlCondition.= " and year(bt.SETUP_D) in ( $this->time ) ";	 
				//$sqlCondition.= " and bt.SETUP_D between '$this->start_time' and '$this->end_time' ";
			}	
			 
			if($this->group_option == $this->GROUP_BY_MANUFACTURER){//制造商多选		
				//echo "group_option = GROUP_BY_MANUFACTURER";
				$this->maker_visible_tag = true;
				//echo "unit_visible_tag =".$this->unit_visible_tag;
				//echo "maker_visible_tag =".$this->maker_visible_tag;
				//$sql.= " GROUP BY db.BATTERYTYPE_V )  g_table ,(SELECT @row :=0)r";
				$sqlGroup= " GROUP BY bt.MANCODE_V ) g_table ";
 				
				$greenSql = sprintf($this->state_sql, 'green_n' , 'dm.RECORD_ID', $this->GREEN );
				$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'dm.RECORD_ID',  $this->YELLOW);
				$redSql = sprintf($this->state_sql, 'red_n' , 'dm.RECORD_ID', $this->RED );
				
				$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cg', 'cg.RECORD_ID',   'bt.MANCODE_V');
				$greenSql=$greenSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cy', 'cy.RECORD_ID',   'bt.MANCODE_V');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cr', 'cr.RECORD_ID',   'bt.MANCODE_V');
				$redSql=$redSql.$sqlCondition.$group_condition;
 
			}elseif($this->group_option == $this->GROUP_BY_SIZE){//电池规格多选
				 
				$this->guige_visible_tag = true;
				$sqlGroup = " GROUP BY bt.BATTERYTYPE_V ) g_table ";
				$greenSql = sprintf($this->state_sql, 'green_n' , 'db.RECORD_ID', $this->GREEN );
				$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'db.RECORD_ID',  $this->YELLOW);
				$redSql = sprintf($this->state_sql, 'red_n' , 'db.RECORD_ID', $this->RED );
				
				$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cg', 'cg.RECORD_ID',   'bt.BATTERYTYPE_V');
				$greenSql=$greenSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cy', 'cy.RECORD_ID',   'bt.BATTERYTYPE_V');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cr', 'cr.RECORD_ID',   'bt.BATTERYTYPE_V');
				$redSql=$redSql.$sqlCondition.$group_condition;
			}elseif($this->group_option == $this->GROUP_BY_TIME){//电池规格多选
				$this->time_visible_tag = true;
							$sqlGroup = " GROUP BY year(bt.SETUP_D) ) g_table ";
				$greenSql = sprintf($this->state_sql, 'green_n' , ' year(bt.SETUP_D) SETUP_D', $this->GREEN );
				$yellowSql = sprintf($this->state_sql, 'yellow_n' , ' year(bt.SETUP_D) SETUP_D',  $this->YELLOW);
				$redSql = sprintf($this->state_sql, 'red_n' , ' year(bt.SETUP_D) SETUP_D', $this->RED );
		
				$group_condition  = sprintf($this->condition_sql, ' year(bt.SETUP_D) ','cg', ' cg.SETUP_D',   ' year(bt.SETUP_D) ');
				$greenSql=$greenSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, ' year(bt.SETUP_D) ','cy', ' cy.SETUP_D',   ' year(bt.SETUP_D) ');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'year(bt.SETUP_D) ','cr', ' cr.SETUP_D ',   ' year(bt.SETUP_D) ');
				$redSql=$redSql.$sqlCondition.$group_condition;
				
				
			}elseif( ($user_unit_level < $this->LEVEL_PROVINCE ||$user_unit_level == $this->LEVEL_BASE)&& $no_unit){ //super user and no option
				//select all data
				 
				$this->group_option = $this->GROUP_BY_NOTHING;
				$userid = Yii::app()->user->id;
				$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
				$unit_level = $user->unit_level;
				$unit_id = $user->unit_id;
				//三项都不显示
				$this->maker_visible_tag = false;
				$this->guige_visible_tag = false;
				$this->unit_visible_tag = false;
				$this->time_visible_tag = false;
				
				//bug 4.19 这里只有两种情况，基站管理员和超级管理员
				if($user->unit_level == 0){
				$state ="SELECT count(1) From  bt_batterys where status_n = %d   " ;

				$sql_fmt = "SELECT @row := @row +1 AS id, count(1) , (%s) as green_n, (%s) as yellow_n, (%s) as red_n
						From bt_batterys, (SELECT @row :=0)r ";
				}else if($user->unit_level == 4){
					$state ="SELECT count(1) From  bt_batterys where status_n = %d  and  base_n in ( $unit_id )" ;
					$sql_fmt = "SELECT @row := @row +1 AS id, count(1) , (%s) as green_n, (%s) as yellow_n, (%s) as red_n
						From bt_batterys  , (SELECT @row :=0)r where  base_n in ( $unit_id )";					
				}
				$greenSql = sprintf($state, 0);
				$yellowSql = sprintf($state, 1);
				$redSql = sprintf($state, 2);
				
				$sql = sprintf($sql_fmt, $greenSql, $yellowSql,$redSql);
				$sql_count = "select count(1) from bt_batterys";
			}elseif($this->group_option == $this->GROUP_BY_PROVINCE){//省级比较
				//echo "group_option = GROUP_BY_PROVINCE";
					$this->unit_visible_tag = true;	
					$sqlGroup= " GROUP BY b.province_id ) g_table ";
					$greenSql = sprintf($this->state_sql, 'green_n' , 'b.province_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.province_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.province_id', $this->RED );
				
					$group_condition  = sprintf($this->condition_sql, 'b.province_id','cg', 'cg.province_id',   'b.province_id');
					$greenSql=$greenSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.province_id','cy', 'cy.province_id',   'b.province_id');
					$yellowSql=$yellowSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.province_id','cr', 'cr.province_id',   'b.province_id');
					$redSql=$redSql.$sqlCondition.$group_condition;
				
			}else if($this->group_option == $this->GROUP_BY_CITY){//S市级	
				
					$this->unit_visible_tag = true;
					//$sql.= " GROUP BY b.city_id ) g_table ,(SELECT @row :=0)r";
					$sqlGroup= " GROUP BY b.city_id ) g_table ";
						
					$greenSql = sprintf($this->state_sql, 'green_n' , 'b.city_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.city_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.city_id', $this->RED );
				
					$group_condition  = sprintf($this->condition_sql, 'b.city_id','cg', 'cg.city_id', 'b.city_id');
					$greenSql=$greenSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.city_id','cy', 'cy.city_id', 'b.city_id');
					$yellowSql=$yellowSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.city_id','cr', 'cr.city_id', 'b.city_id');
					$redSql=$redSql.$sqlCondition.$group_condition;
 
			}else if($this->group_option == $this->GROUP_BY_COUNTY){//县级
					$this->unit_visible_tag = true;
				 
					$sqlGroup= " GROUP BY b.county_id ) g_table ";
					$greenSql = sprintf($this->state_sql, 'green_n' , 'b.county_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.county_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.county_id', $this->RED );
				
					$group_condition  = sprintf($this->condition_sql, 'b.county_id','cg', 'cg.county_id', 'b.county_id');
					$greenSql=$greenSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.county_id','cy', 'cy.county_id', 'b.county_id');
					$yellowSql=$yellowSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.county_id','cr', 'cr.county_id', 'b.county_id');
					$redSql=$redSql.$sqlCondition.$group_condition;
				
			}else if($this->group_option == $this->GROUP_BY_BASE){//站点
				
					$this->unit_visible_tag = true;
					$sqlGroup= " GROUP BY b.base_id ) g_table ";
				
					$greenSql = sprintf($this->state_sql, 'green_n' , 'b.base_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.base_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.base_id', $this->RED );
						
					$group_condition  = sprintf($this->condition_sql, 'b.base_id','cg', 'cg.base_id', 'b.base_id');
					$greenSql=$greenSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.base_id','cy', 'cy.base_id', 'b.base_id');
					$yellowSql=$yellowSql.$sqlCondition.$group_condition;
					$group_condition  = sprintf($this->condition_sql, 'b.base_id','cr', 'cr.base_id', 'b.base_id');
					$redSql=$redSql.$sqlCondition.$group_condition;				 
			} 	else{	
				//goto select the condition by hyq , 在details 页面直接按enter键
						 
						$this->actionAdmin();
			}
		//$size = $_POST['select-size'];
		if(isset($_GET['status'])){
			$this->status = $_GET['status'];
			$this->option_id =  $_GET['option_id'];
			if( $this->group_option == $this->GROUP_BY_NOTHING){ //super user || base user and no option
				 
				if($user_unit_level == 0){
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}else if($user_unit_level == 4){
					$sql_details.= " b.base_id in ($user_unit_id)";
					$count_details.= " b.base_id in ($user_unit_id)";
				}

			}elseif($this->group_option >= $this->GROUP_BY_PROVINCE){ //单位多选或者分组
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					$sql_details.= " b.province_id in ($this->option_id)";
					$count_details.= " b.province_id in ($this->option_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->option_id)";
					$count_details.= " b.city_id in ($this->option_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->option_id)";
					$count_details.= " b.county_id in ($this->option_id)";
				}elseif($this->level == $this->LEVEL_BASE) {
					//站点
					$sql_details.= " b.base_id in ($this->option_id)";
					$count_details.= " b.base_id in ($this->option_id)";
				}else{
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->manufacturer)";
					$count_details.= " and  dm.RECORD_ID  in ($this->manufacturer)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					$sql_details.= " and db.RECORD_ID in ($this->bt_type)";
					$count_details.= " and db.RECORD_ID in ($this->bt_type)";
				}	
				if ($this->time != null && $this->time != "" )
					{
						$sql_details.= " and year(bt.SETUP_D) in ($this->time)";
						$count_details.= " and year(bt.SETUP_D) in ($this->time)";
					}
			}elseif($this->group_option == $this->GROUP_BY_SIZE){// 多选
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					//echo "test unit";
					$sql_details.= " b.province_id in ($this->province_id)";
					$count_details.= " b.province_id in ($this->province_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->city_id)";
					$count_details.= " b.city_id in ($this->city_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->county_id)";
					$count_details.= " b.county_id in ($this->county_id)";
				}elseif ($this->level == $this->LEVEL_BASE){
					//站点
					$sql_details.= " b.base_id in ($this->base_id)";
					$count_details.= " b.base_id in ($this->base_id)";
				}else{
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->manufacturer)";
					$count_details.= " and  dm.RECORD_ID  in ($this->manufacturer)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					
					$sql_details.= " and db.RECORD_ID in ($this->option_id)";
					$count_details.= " and db.RECORD_ID in ($this->option_id)";
				}
				if ($this->time != null && $this->time != "" )
					{
						$sql_details.= " and year(bt.SETUP_D) in ($this->time)";
						$count_details.= " and year(bt.SETUP_D) in ($this->time)";
					}
			}elseif($this->group_option ==$this->GROUP_BY_MANUFACTURER){
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					//echo "test unit";
					$sql_details.= " b.province_id in ($this->province_id)";
					$count_details.= " b.province_id in ($this->province_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->city_id)";
					$count_details.= " b.city_id in ($this->city_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->county_id)";
					$count_details.= " b.county_id in ($this->county_id)";
				}elseif ($this->level == $this->LEVEL_BASE){
					//站点
					$sql_details.= " b.base_id in ($this->base_id)";
					$count_details.= " b.base_id in ($this->base_id)";
				}else{
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->option_id)";
					$count_details.= " and  dm.RECORD_ID  in ($this->option_id)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					$sql_details.= " and db.RECORD_ID in ($this->bt_type)";
					$count_details.= " and db.RECORD_ID in ($this->bt_type)";
				}
							if ($this->time != null && $this->time != "" )
					{
						$sql_details.= " and year(bt.SETUP_D) in ($this->time)";
						$count_details.= " and year(bt.SETUP_D) in ($this->time)";
					}
			}elseif($this->group_option ==$this->GROUP_BY_TIME){
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					//echo "test unit";
					$sql_details.= " b.province_id in ($this->province_id)";
					$count_details.= " b.province_id in ($this->province_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->city_id)";
					$count_details.= " b.city_id in ($this->city_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->county_id)";
					$count_details.= " b.county_id in ($this->county_id)";
				}elseif ($this->level == $this->LEVEL_BASE){
					//站点
					$sql_details.= " b.base_id in ($this->base_id)";
					$count_details.= " b.base_id in ($this->base_id)";
				}else{
					$sql_details.= " 1 ";
					$count_details.= " 1 ";
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->manufacturer)";
					$count_details.= " and  dm.RECORD_ID  in ($this->manufacturer)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					$sql_details.= " and db.RECORD_ID in ($this->bt_type)";
					$count_details.= " and db.RECORD_ID in ($this->bt_type)";
				}
				if ($this->time != null && $this->time != "" )
					{
						$sql_details.= " and year(bt.SETUP_D) in ($this->option_id)";
						$count_details.= " and year(bt.SETUP_D) in ($this->option_id)";
					}
			}else{
				
				/*
				$sql_details.= " b.base_id in ($this->option_id)";
				$count_details.= " b.base_id in ($this->option_id)"; */
				if($this->level == $this->LEVEL_PROVINCE){
					//省级比较
					$sql_details.= " b.province_id in ($this->option_id)";
					$count_details.= " b.province_id in ($this->option_id)";
				}else if($this->level == $this->LEVEL_CITY){
					//市级
					$sql_details.= " b.city_id in ($this->option_id)";
					$count_details.= " b.city_id in ($this->option_id)";
				}else if($this->level == $this->LEVEL_COUNTY){
					//县区级
					$sql_details.= " b.county_id in ($this->option_id)";
					$count_details.= " b.county_id in ($this->option_id)";
				}elseif($this->level == $this->LEVEL_BASE) {
					//站点
					$sql_details.= " b.base_id in ($this->option_id)";
					$count_details.= " b.base_id in ($this->option_id)";
				}else{
					//站点
					 
					$sql_details.= " b.base_id in ($user->unit_id)";
					$count_details.= " b.base_id in ($this->unit_id)";
 
				}
				if($this->manufacturer != null && $this->manufacturer != ""){
					$sql_details.= " and dm.RECORD_ID  in ($this->manufacturer)";
					$count_details.= " and  dm.RECORD_ID  in ($this->manufacturer)";
				}
				if($this->bt_type != null && $this->bt_type != ""){
					$sql_details.= " and db.RECORD_ID in ($this->option_id)";
					$count_details.= " and db.RECORD_ID in ($this->option_id)";
				}
				if ($this->time != null && $this->time != "" )
					{
						$sql_details.= " and year(bt.SETUP_D) in ($this->time) ";
						$count_details.= " and year(bt.SETUP_D) in ($this->time) ";
					}
			} 	

			$sql_details.= " and bt.STATUS_N = $this->status";
			$count_details.= " and bt.STATUS_N = $this->status";
			
		}else{

			$sql_details.= " 0";
			$count_details.= " 0";
		}
		//to set the battery
		$battery;
		if(isset($_GET['record_id'])){
			$gid = $_GET['record_id'];
			//echo $gid;
			 $battery = BtBattery::model()->findAll("GROUP_V=:gid",array(
				":gid" => $gid ));
			  
		}else{
			$battery = BtBattery::model()->findAll('GROUP_V = -1');
		}
			//concat the sql 4 total
		if( $this->group_option != $this->GROUP_BY_NOTHING){ //super user  		
			$sql  = $this->sql_fields.$this->tables." LEFT join ".$greenSql." LEFT join ".$yellowSql." LEFT join ".$redSql." where ".$sqlCondition.$sqlGroup.",(SELECT @row :=0)r";
			$sql_count  = $this->sql_count.$this->tables." where ".$sqlCondition.$sqlGroup;
 
		}
		//echo $sql_details;
		//echo "asdfasf".$sql;
		try {
				$count_record=Yii::app()->db->createCommand($sql_count)->queryScalar();
				////echo $count_record."aaa";
				//echo $sql;
				$dataProvider = new CSqlDataProvider($sql,
						array(
								'totalItemCount'=>$count_record,
								'pagination'=>array(
								'pageSize'=>10,
								),/*
								'sort'=>array(
										'attributes'=>array(
												'manname',
												'unitSearch'=>array(
														'asc'=>'province_name , city_name , county_name , base_name asc' ,
														'desc'=>'province_name, city_name, county_name , base_name desc',
												), 
												'btype'=>array(
														'asc'=>'BATTERYTYPE_V',
														'desc'=>'BATTERYTYPE_V desc',
												), 
												
										),
								),*/
						));

				//count data
				//var_dump($sql_details);
				//echo $sql_details;
				$count=Yii::app()->db->createCommand($count_details)->queryScalar();
				$dataProvider_details = new CSqlDataProvider($sql_details,
						array(
								'totalItemCount'=>$count,
								'sort'=>array(
										'attributes'=>array(
												
												 'unit_search'=>array(
													'asc'=>'province_name, city_name, county_name, base_name',
													'desc'=>'province_name desc, city_name desc, county_name desc, base_name desc',
										), 'GROUPNAME_V', 'MANNAME_V','bt_date','SORTNAME_V','BATTERYTYPE_V','BASE_TYPE',
										),
								),
								'pagination'=>array(
								'pageSize'=>10,
								), 
						));
		} catch (Exception $e) {
			//echo "sql exception";
			//var_dump($dataProvider);
			//return;
		}

	//var_dump($dataProvider);
		$this->render('details',array(
				'dataProvider'=>$dataProvider,
				'dataProvider_details'=>$dataProvider_details,	
				'battery'=>$battery,
		));
	}
	
	
	
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new MtsysBase;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		$provinces = NULL;
		$citys = NULL;
		$countys = NULL; 
		
		if(isset($_POST['MtsysBase']))
		{
			$model->attributes=$_POST['MtsysBase'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->BASE_ID));
		}
		
		
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$unit_level = $user->unit_level;
		$unit_id = $user->unit_id;
		
		switch($unit_level){
			case 0:
			$provinces = MtsysProvince::model()->findAll();
			break;
			case 1:
			$provinces = MtsysProvince::model()->findAll("PROVINCE_ID=:pid",array(
				":pid" => $unit_id
			));
			$citys = MtsysCity::model()->findAll('PROVINCE_ID=:PID',array(':PID'=>$unit_id));
			break;
			case 2:
			$citys = MtsysCity::model()->findAll('CITY_ID=:CID',array(':CID'=>$unit_id));
			$provinces = MtsysProvince::model()->findAll("PROVINCE_ID=:pid",array(
				":pid" => $citys->PROVINCE_ID
			));
			break;
			case 3:
			$countys = MtsysCounty::model()->findAll('COUNTY_ID=:CID',array(':CID'=>$unit_id));
			$citys = MtsysCity::model()->findAll('CITY_ID=:CID',array(':CID'=>$countys->CITY_ID));
			$provinces = MtsysProvince::model()->findAll("PROVINCE_ID=:pid",array(
				":pid" => $citys->PROVINCE_ID
			));
			break;
		}
			
		
		$this->render('create',array(
			'model'=>$model,			
			'provinces'=>$provinces, 
			'citys' => $citys,
			'countys' => $countys,
		));
	}
	
	public function actionGetCitys()
	{
		$this->level=3;
		$this->province_id =(int)$_POST['PROVINCE_ID'];

		
		$rs = '';
		$data = MtsysCity::model()->findAll('PROVINCE_ID=:PID',array(':PID'=>$_POST['PROVINCE_ID']));
        $data=CHtml::listData($data,'CITY_ID','CITY_NAME');
		
        if(!empty($data)){
			foreach($data as $value=>$name){
        		$rs .= CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
        	}
		}
		$rs .= CHtml::tag('option',array('value'=>'-1'),CHtml::encode('无下级单位'),true);
		
		//echo $rs;
	}
	
	public function actionGetCountys()
	{
		$this->level = 2;
		$this->city_id =(int)$_POST['CITY_ID'];
		$data=MtsysCounty::model()->findAll('CITY_ID=:CID',
                      array(':CID'=>(int)$_POST['CITY_ID']));
    
        $data=CHtml::listData($data,'COUNTY_ID','COUNTY_NAME');
        if(!empty($data)){
			foreach($data as $value=>$name){
        		//echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
        	}
		}
		//echo CHtml::tag('option',array('value'=>'-1'),CHtml::encode('无下级单位'),true);
	}
	
	
	
	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new MtsysBase;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		/*
		if(isset($_POST['MtsysBase']))
		{
			//var_dump($_POST['MtsysBase']);
			exit;
				$this->redirect(array('details',));
		}
		*/
		$provinces =null;
		$base = null;
		$cities= null;
		$counties= null;
		$unit_level;
		
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$unit_level = $user->unit_level;
		$unit_id = $user->unit_id;
		
		if($unit_level == 0){ 	//super
			$provinces = MtsysProvince::model()->findAll();
		
		}elseif($unit_level == 1){ 	//province
			$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID ',array(':PID'=>$unit_id));
				
		}elseif($unit_level == 2){	//city
			$cities = MtsysCity::model()->findByPk($unit_id);
			$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID ',array(':PID'=>$cities->PROVINCE_ID));
			
		}elseif($unit_level == 3){	//county
			$couties = MtsysCounty::model()->findByPk($unit_id);
			$cities = MtsysCity::model()->findByPk($couties->CITY_ID);
			$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID ',array(':PID'=>$cities->PROVINCE_ID));	
					
		}else{		//base
//			//echo $unit_id;
//			$base = MtsysBase::model()->findAll('BASE_ID=:ID ',array(':ID'=>$unit_id));
			

			$base = MtsysBase::model()->findAll('BASE_ID in (:bids)', array(':bids'=>$user->unit_id));
 			if($base != null){
			//var_dump($base);;
				$couties = MtsysCounty::model()->findAll('COUNTY_ID=:PID ',array(':PID'=>$base[0]->COUNTY_ID));
				$cities =  MtsysCity::model()->findAll('CITY_ID=:PID ',array(':PID'=>$base[0]->CITY_ID));			
				$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID ',array(':PID'=>$base[0]->PROVINCE_ID));

 			}
		}
			
		//$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID ',array(':PID'=>$_POST['PROVINCE_ID']));
		
		//制造商
		$manufaturer =  DictManufacturer::model()->findAll();
		
		//电池规格
		$dictbattery =  DictBattery::model()->findAll();
		
		// 创建年份 in bt_batterys
		$batterys = BtBatterys::model()->findAll();
		
		$this->render('admin',array(
				'unit_level'=>$unit_level,
				'cities' =>$cities,
				'counties' =>$counties,
				'base' =>$base,
				'model'=>$model,
				'provinces'=>$provinces,
				'manufaturer'=>$manufaturer,
				'dictbattery'=>$dictbattery,
				'batterys'=>$batterys,
		));

	}

	public function actionCharts()
	{
	
		$dataProvider_details ;
		$dataProvider;
		$group_option = -1; // -1 : no muti; 0 : unit ; 1 :manufacturer; 2 :size;
		$sql_count;
		$sql;
		$greenSql;
		$yellowSql;
		$redSql;
		$sqlCondition = '';
		$sqlGroup = '';
		
 
		$no_unit = 0;
		$str_unit ;
		$manufacturer;
		$size;
		$str_time;
		$status;
		
		//获取用户所属单位的级别
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$user_unit_level = $user->unit_level;
		$user_unit_id = $user->unit_id;
		
		if(isset($_GET['redirect']))
		{
			$str_unit =$_GET['select_unit'];
			$manufacturer = $_GET['select_maker'];
			$size = $_GET['select_size'];
			$str_time = $_GET['select_timer'];
//			echo 'redirect';
		
		}else {
			$str_unit =$_POST['select-unit'];
			$manufacturer = $_POST['select-maker'];
			$size = $_POST['select-size'];
			$str_time = $_POST['select-timer'];
		}
		$this->select_unit = $str_unit;
		$this->select_maker = $manufacturer;
		$this->select_size = $size;
		$this->select_timer = $str_time;
		////echo 'unit'.$_POST['select-unit'];
		//$str_unit =$_POST['select-unit'];
		//$str_unit ="14,3,1|14,4,3";
		////echo 	$str_unit."str_unit".strlen($str_unit);
	
		
		if(isset($manufacturer))
		{
			//echo 'manufacturer'.$_POST['select-manufacturer'];
			//$manufacturer = $_POST['select-manufacturer'];
		
			if (strpos( $manufacturer , '|') !== false) {
				$this->group_option = 1;
			}		
			$this->manufacturer = str_replace("|", " , " , $manufacturer);
		}

		if(isset($size))
		{
			//echo "size=".$size;
			$this->select_size = $size;
			if (strpos( $size , '|') !== false) {
				$this->group_option = $this->GROUP_BY_SIZE;
			}
			$this->bt_type = str_replace("|", " , " , $size);
		}
		
		if(isset($str_time))
		{
		
			$this->select_timer = $str_time;
			if (strpos( $str_time , '|') !== false) {
				$this->group_option = $this->GROUP_BY_TIME;
			}
			$this->time = str_replace("|", " , " , $str_time);
		}
		

		if(isset($str_unit)){
			//echo "$str_unit=".$str_unit;
			$this->select_unit = $str_unit;
			if ($str_unit == null || strlen($str_unit) ==0  ) {
				//echo 	$this->group_option."level";
				$no_unit = 1;
				if($user_unit_level >= $this->LEVEL_PROVINCE ){
					if($user_unit_level == $this->LEVEL_PROVINCE){
						$this->level =$this->LEVEL_PROVINCE;
						$this->province_id  = $user_unit_id ;
						//echo "group_option".$this->group_option;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_PROVINCE;
					}elseif($user_unit_level == $this->LEVEL_CITY){
						$this->level =$this->LEVEL_CITY;
						$this->city_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_CITY;
					}elseif($user_unit_level == $this->LEVEL_COUNTY){
						$this->level =$this->LEVEL_COUNTY;
						$this->county_id  = $user_unit_id ;
						if($this->group_option < 0) //
							$this->group_option = $this->GROUP_BY_COUNTY;
					}elseif($user_unit_level == $this->LEVEL_BASE){
		
						/*$this->level =$this->LEVEL_BASE;
						 $this->base_id  = $user_unit_id ;
		
						if($this->group_option < 0) //
						$this->group_option = $this->GROUP_BY_BASE;
						*/
					}
				}
			}else{
					
				$this->unit_visible_tag = true;
				//split and split the unit string
				$units = explode("|", $str_unit);
				$numbers = count($units); //count
					
				$all_units = explode(",", $units[0]);
				$this->level = count($all_units); //count the level
		
				$this->province_id = "";
				$this->city_id = "";
				$this->county_id = "";
				$this->base_id = "";
					
				//parse the units_str
				$flag_for_choise_level_upper_unit_level = 0;
				//parse the units_str
				foreach ($units as $unit){
					//echo $unit;
					$all_units = explode(",", $unit);
					$this->level = count($all_units); //count the level
						
					if($this->level < intval($user_unit_level)){
						//如果选择的级别高于用户级别，使用用户级别
						$flag_for_choise_level_upper_unit_level = 1;
						break;
					}else{
						//如果用户选择级别低于用户级别，使用选择数据
						$flag_for_choise_level_upper_unit_level = 0;
					}
		
					//else
					{
						if($this->level == $this->LEVEL_PROVINCE){ //province
							$this->province_id .= ','.$all_units[0];
						}elseif($this->level == $this->LEVEL_CITY){ //city
							$this->province_id .= ','.$all_units[0];
							$this->city_id .= ','.$all_units[1];
						}elseif($this->level == $this->LEVEL_COUNTY){ //county
							$this->province_id .= ','.$all_units[0];
							$this->city_id .= ','.$all_units[1];
							$this->county_id .= ','.$all_units[2];
						}else if($this->level == $this->LEVEL_BASE){ //base
							$this->province_id .= ','.$all_units[0];
							$this->city_id .= ','.$all_units[1];
							$this->county_id .= ','.$all_units[2];
							$this->base_id .= ','.$all_units[3];
						}
					}
				}
		
				if($flag_for_choise_level_upper_unit_level == 0){
					//如果用户选择级别低于用户级别，使用选择数据
					$this->province_id = substr($this->province_id, 1, strlen($this->province_id)-1);
					$this->city_id = substr($this->city_id, 1, strlen($this->city_id)-1);
					$this->county_id = substr($this->county_id, 1, strlen($this->county_id) -1 );
					$this->base_id = substr($this->base_id, 1, strlen($this->base_id)-1);
				}else{
					//如果选择的级别高于用户级别，使用用户级别
					if($user_unit_level >= $this->LEVEL_PROVINCE ){
						if($user_unit_level == $this->LEVEL_PROVINCE){
							$this->level =$this->LEVEL_PROVINCE;
							$this->province_id  = $user_unit_id ;
							//echo "group_option".$this->group_option;
							if($this->group_option < 0) //
								$this->group_option = $this->GROUP_BY_PROVINCE;
						}elseif($user_unit_level == $this->LEVEL_CITY){
							$this->level =$this->LEVEL_CITY;
							$this->city_id  = $user_unit_id ;
							if($this->group_option < 0) //
								$this->group_option = $this->GROUP_BY_CITY;
						}elseif($user_unit_level == $this->LEVEL_COUNTY){
							$this->level =$this->LEVEL_COUNTY;
							$this->county_id  = $user_unit_id ;
							if($this->group_option < 0) //
								$this->group_option = $this->GROUP_BY_COUNTY;
						}elseif($user_unit_level == $this->LEVEL_BASE){
		
							$this->level =$this->LEVEL_BASE;
							$this->base_id  = $user_unit_id ;
							if($this->group_option < 0) //
								$this->group_option = $this->GROUP_BY_BASE;
						}
					}
				}
		
			}
		}
		
		
		if($this->level == $this->LEVEL_PROVINCE){
			//省级比较
			//echo "LEVEL_PROVINCE";
			if($this->group_option < 0) //
				$this->group_option = $this->GROUP_BY_PROVINCE;
			$this->cond_unit = $this->province_id;
			$sqlCondition.= " b.province_id in ($this->province_id) ";
			//$sql_count.= " b.province_id in ($this->province_id)";
		}else if($this->level == $this->LEVEL_CITY){
			//S市级
			//echo "LEVEL_CITY";
			if($this->group_option < 0) //
				$this->group_option = $this->GROUP_BY_CITY;
			$this->cond_unit = $this->city_id;
			$sqlCondition.= " b.city_id in ($this->city_id) ";
			//$sql_count.= " b.city_id in ($this->province_id)";
		}else if($this->level == $this->LEVEL_COUNTY){
			//县级
			$this->cond_unit = $this->county_id;
			if($this->group_option < 0) //
				$this->group_option = $this->GROUP_BY_COUNTY;
			$sqlCondition.= " b.county_id in ($this->county_id) ";
			//$sql_count.= " b.county_id in ($this->county_id)";
		}elseif($this->level == $this->LEVEL_BASE) {
			//站点			s
			$this->cond_unit = $this->base_id;
			$sqlCondition.= " b.base_id in ($this->base_id) ";
			if($this->group_option < 0) //
				$this->group_option = $this->GROUP_BY_BASE;
			//$sql_count.= " b.base_id in ($this->base_id)";
		}else{
			$this->cond_unit = "";
			$bases = MtsysBase::model()->searchByUserid();
			if($bases != null){
				$basesData = $bases->getData();
				$this->base_id = null;
				foreach($basesData as $base ){
					$this->base_id .=$base["BASE_ID"].' , ';
				}
				if($this->base_id != null) {
					$this->base_id[strlen($this->base_id)-2] =' ';
				}
			}
			if($this->base_id != null && $this->base_id != ""){
				$sqlCondition.= " b.base_id in ($this->base_id) ";
			}else {
				$sqlCondition.= " b.base_id in ( -1 ) ";
			}
		}
	 
			
		if($this->manufacturer != null && $this->manufacturer != ""){
			$sqlCondition.= " and dm.RECORD_ID  in ($this->manufacturer) ";
			$this->maker_visible_tag = true;
		}
		if($this->bt_type != null && $this->bt_type != ""){
			$this->guige_visible_tag = true;
			$sqlCondition.= " and db.RECORD_ID in ($this->bt_type) ";
		}
		
		if ($this->time != null && $this->time != "") {
			$this->time_visible_tag = true;
			//SELECT year (bt.SETUP_D) FROM `bt_batterys` bt where  2012 =  year( bt.SETUP_D) or  2013  = year( bt.SETUP_D)   GROUP BY year (bt.SETUP_D)
			$sqlCondition.= " and year(bt.SETUP_D) in ( $this->time ) ";
			//$sqlCondition.= " and bt.SETUP_D between '$this->start_time' and '$this->end_time' ";
		}
		
			if($this->group_option == $this->GROUP_BY_MANUFACTURER){//制造商多选
				//echo "group_option = GROUP_BY_MANUFACTURER";
					$this->maker_visible_tag = true;
					//echo "unit_visible_tag =".$this->unit_visible_tag;
					//echo "maker_visible_tag =".$this->maker_visible_tag;
					//$sql.= " GROUP BY db.BATTERYTYPE_V )  g_table ,(SELECT @row :=0)r";
					$sqlGroup= " GROUP BY bt.MANCODE_V ) g_table ";
 		
				$greenSql = sprintf($this->state_sql, 'green_n' , 'dm.RECORD_ID', $this->GREEN );
								$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'dm.RECORD_ID',  $this->YELLOW);
								$redSql = sprintf($this->state_sql, 'red_n' , 'dm.RECORD_ID', $this->RED );
		
								$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cg', 'cg.RECORD_ID',   'bt.MANCODE_V');
										$greenSql=$greenSql.$sqlCondition.$group_condition;
										$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cy', 'cy.RECORD_ID',   'bt.MANCODE_V');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'dm.RECORD_ID','cr', 'cr.RECORD_ID',   'bt.MANCODE_V');
				$redSql=$redSql.$sqlCondition.$group_condition;
		
			}elseif($this->group_option == $this->GROUP_BY_SIZE){//电池规格多选
				
			$this->guige_visible_tag = true;
			$sqlGroup = " GROUP BY bt.BATTERYTYPE_V ) g_table ";
				$greenSql = sprintf($this->state_sql, 'green_n' , 'db.RECORD_ID', $this->GREEN );
				$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'db.RECORD_ID',  $this->YELLOW);
				$redSql = sprintf($this->state_sql, 'red_n' , 'db.RECORD_ID', $this->RED );
				$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cg', 'cg.RECORD_ID',   'bt.BATTERYTYPE_V');
				$greenSql=$greenSql.$sqlCondition.$group_condition;
				$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cy', 'cy.RECORD_ID',   'bt.BATTERYTYPE_V');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'db.RECORD_ID','cr', 'cr.RECORD_ID',   'bt.BATTERYTYPE_V');
				$redSql=$redSql.$sqlCondition.$group_condition;
			}elseif($this->group_option == $this->GROUP_BY_TIME){//电池规格多选
							$this->time_visible_tag = true;
							$sqlGroup = " GROUP BY year(bt.SETUP_D) ) g_table ";
				$greenSql = sprintf($this->state_sql, 'green_n' , ' year(bt.SETUP_D) SETUP_D', $this->GREEN );
				$yellowSql = sprintf($this->state_sql, 'yellow_n' , ' year(bt.SETUP_D) SETUP_D',  $this->YELLOW);
				$redSql = sprintf($this->state_sql, 'red_n' , ' year(bt.SETUP_D) SETUP_D', $this->RED );
		
				$group_condition  = sprintf($this->condition_sql, ' year(bt.SETUP_D) ','cg', ' cg.SETUP_D',   ' year(bt.SETUP_D) ');
				$greenSql=$greenSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, ' year(bt.SETUP_D) ','cy', ' cy.SETUP_D',   ' year(bt.SETUP_D) ');
				$yellowSql=$yellowSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'year(bt.SETUP_D) ','cr', ' cr.SETUP_D ',   ' year(bt.SETUP_D) ');
				$redSql=$redSql.$sqlCondition.$group_condition;
				
			}elseif( ($user_unit_level < $this->LEVEL_PROVINCE ||$user_unit_level == $this->LEVEL_BASE) && $no_unit){ //super user and no option
			//select all data
		
			$this->group_option = $this->GROUP_BY_NOTHING;
			$userid = Yii::app()->user->id;
			$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
			$unit_level = $user->unit_level;
			$unit_id = $user->unit_id;
			//三项都不显示
			$this->maker_visible_tag = false;
			$this->guige_visible_tag = false;
			$this->unit_visible_tag = false;
			$this->time_visible_tag = false;
		
							//bug 4.19 这里只有两种情况，基站管理员和超级管理员
				if($user->unit_level == 0){
				$state ="SELECT count(1) From  bt_batterys where status_n = %d   " ;

				$sql_fmt = "SELECT @row := @row +1 AS id, count(1) , (%s) as green_n, (%s) as yellow_n, (%s) as red_n
						From bt_batterys, (SELECT @row :=0)r ";
				}else if($user->unit_level == 4){
					$state ="SELECT count(1) From  bt_batterys where status_n = %d  and  base_n in ( $unit_id )" ;
					$sql_fmt = "SELECT @row := @row +1 AS id, count(1) , (%s) as green_n, (%s) as yellow_n, (%s) as red_n
						From bt_batterys  , (SELECT @row :=0)r where  base_n in ( $unit_id )";					
				}
				$greenSql = sprintf($state, 0);
				$yellowSql = sprintf($state, 1);
			$redSql = sprintf($state, 2); 
		
			$sql = sprintf($sql_fmt, $greenSql, $yellowSql,$redSql);
			$sql_count = "select count(1) from bt_batterys";
			}elseif($this->group_option == $this->GROUP_BY_PROVINCE){//省级比较
				//echo "group_option = GROUP_BY_PROVINCE";
						$this->unit_visible_tag = true;
				$sqlGroup= " GROUP BY b.province_id ) g_table ";
				$greenSql = sprintf($this->state_sql, 'green_n' , 'b.province_id', $this->GREEN );
						$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.province_id',  $this->YELLOW);
						$redSql = sprintf($this->state_sql, 'red_n' , 'b.province_id', $this->RED );
		
						$group_condition  = sprintf($this->condition_sql, 'b.province_id','cg', 'cg.province_id',   'b.province_id');
						$greenSql=$greenSql.$sqlCondition.$group_condition;
								$group_condition  = sprintf($this->condition_sql, 'b.province_id','cy', 'cy.province_id',   'b.province_id');
								$yellowSql=$yellowSql.$sqlCondition.$group_condition;
								$group_condition  = sprintf($this->condition_sql, 'b.province_id','cr', 'cr.province_id',   'b.province_id');
								$redSql=$redSql.$sqlCondition.$group_condition;
		
			}elseif($this->group_option == $this->GROUP_BY_CITY){//S市级
			$this->unit_visible_tag = true;
						//$sql.= " GROUP BY b.city_id ) g_table ,(SELECT @row :=0)r";
							$sqlGroup= " GROUP BY b.city_id ) g_table ";
		
							$greenSql = sprintf($this->state_sql, 'green_n' , 'b.city_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.city_id',  $this->YELLOW);
					$redSql = sprintf($this->state_sql, 'red_n' , 'b.city_id', $this->RED );
		
									$group_condition  = sprintf($this->condition_sql, 'b.city_id','cg', 'cg.city_id', 'b.city_id');
									$greenSql=$greenSql.$sqlCondition.$group_condition;
									$group_condition  = sprintf($this->condition_sql, 'b.city_id','cy', 'cy.city_id', 'b.city_id');
									$yellowSql=$yellowSql.$sqlCondition.$group_condition;
									$group_condition  = sprintf($this->condition_sql, 'b.city_id','cr', 'cr.city_id', 'b.city_id');
									$redSql=$redSql.$sqlCondition.$group_condition;
		
						}else if($this->group_option == $this->GROUP_BY_COUNTY){//县级
						$this->unit_visible_tag = true;
							
						$sqlGroup= " GROUP BY b.county_id ) g_table ";
						$greenSql = sprintf($this->state_sql, 'green_n' , 'b.county_id', $this->GREEN );
					$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.county_id',  $this->YELLOW);
									$redSql = sprintf($this->state_sql, 'red_n' , 'b.county_id', $this->RED );
		
									$group_condition  = sprintf($this->condition_sql, 'b.county_id','cg', 'cg.county_id', 'b.county_id');
						$greenSql=$greenSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'b.county_id','cy', 'cy.county_id', 'b.county_id');
						$yellowSql=$yellowSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'b.county_id','cr', 'cr.county_id', 'b.county_id');
						$redSql=$redSql.$sqlCondition.$group_condition;
		
						}else if($this->group_option == $this->GROUP_BY_BASE){//站点
		
						$this->unit_visible_tag = true;
						$sqlGroup= " GROUP BY b.base_id ) g_table ";
		
						$greenSql = sprintf($this->state_sql, 'green_n' , 'b.base_id', $this->GREEN );
						$yellowSql = sprintf($this->state_sql, 'yellow_n' , 'b.base_id',  $this->YELLOW);
						$redSql = sprintf($this->state_sql, 'red_n' , 'b.base_id', $this->RED );
		
						$group_condition  = sprintf($this->condition_sql, 'b.base_id','cg', 'cg.base_id', 'b.base_id');
						$greenSql=$greenSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'b.base_id','cy', 'cy.base_id', 'b.base_id');
						$yellowSql=$yellowSql.$sqlCondition.$group_condition;
						$group_condition  = sprintf($this->condition_sql, 'b.base_id','cr', 'cr.base_id', 'b.base_id');
						$redSql=$redSql.$sqlCondition.$group_condition;
			} 	else{
			//goto select the condition by hyq , 在details 页面直接按enter键
					$this->actionAdmin();
			}
			//concat the sql 4 total
			if( $this->group_option != $this->GROUP_BY_NOTHING){   //super user and no option and no other option
				$sql  = $this->sql_fields.$this->tables." LEFT join ".$greenSql." LEFT join ".$yellowSql." LEFT join ".$redSql." where ".$sqlCondition.$sqlGroup.",(SELECT @row :=0)r";
				$sql_count = $this->sql_count.$this->tables." where ".$sqlCondition.$sqlGroup;
			}
		////echo $sql;
		try {
			$count_record=Yii::app()->db->createCommand($sql_count)->queryScalar();
			////echo $count_record."aaa";
			$dataProvider = new CSqlDataProvider($sql,
					array(
							'totalItemCount'=>$count_record,
							'pagination'=>array(
							'pageSize'=>$count_record,
							),
					));

		} catch (Exception $e) {
			//echo "sql exception";
			//var_dump($dataProvider);
			return;
		}
		$get_str = "status=".$this->status."&bt_type=".$this->bt_type."&manufacturer=".$this->manufacturer."&level=".$this->level."&cond_unit=".$this->cond_unit."&group_option=".$this->group_option;
		
		//echo $get_str;
		$dataSet = $dataProvider->getData();
		//var_dump($dataSet);
		$this->render('charts',array(
				'get_str'=>$get_str,
				'dataSet'=>$dataSet,

		));
	}
	
	public function actionGetUnitCitys()
	{
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$unit_level = $user->unit_level;
		$unit_id = $user->unit_id;
		
/*		if($unit_level == 0){ 	//super
			$provinces = MtsysProvince::model()->findAll();
		
		}elseif($unit_level == 1){ 	//province
			$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID ',array(':PID'=>$unit_id));
		
		}elseif($unit_level == 2){	//city
			$cities = MtsysCity::model()->findByPk($unit_id);
			$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID ',array(':PID'=>$cities->PROVINCE_ID));
				
		}elseif($unit_level == 3){	//county
			$couties = MtsysCounty::model()->findByPk($unit_id);
			$cities = MtsysCity::model()->findByPk($couty->CITY_ID);
			$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID ',array(':PID'=>$cities->PROVINCE_ID));
				
		}else{		
			
			//echo "$unit_level == 4";
			$base = MtsysBase::model()->findByPk($user->unit_id);
			
			$couties = MtsysCounty::model()->findByPk( $base->COUNTY_ID);
			$cities =  MtsysCity::model()->findByPk($base->CITY_ID);
			$provinces = MtsysProvince::model()->findByPk($base->PROVINCE_ID);
				
		}*/
		
		
		$rs = '';
		
		if($unit_level == 0 || $unit_level == 1){
			$data = MtsysCity::model()->findAll('PROVINCE_ID=:PID',array(':PID'=>$_POST['PROVINCE_ID']));
			$data=CHtml::listData($data,'CITY_ID','CITY_NAME');
		}elseif($unit_level == 2){
			$data = MtsysCity::model()->findAll('CITY_ID=:PID',array(':PID'=>$unit_id));
			$data=CHtml::listData($data,'CITY_ID','CITY_NAME');
		}elseif($unit_level == 3){
			$county = MtsysCounty::model()->findByPk($unit_id);
			$data = MtsysCity::model()->findAll('CITY_ID=:PID',array(':PID'=>$county->CITY_ID));
			$data=CHtml::listData($data,'CITY_ID','CITY_NAME');			
		}elseif($unit_level == 4){
			$base = MtsysBase::model()->findByPk($unit_id);
			$data = MtsysCity::model()->findAll('CITY_ID=:PID',array(':PID'=>$base->CITY_ID));
			$data=CHtml::listData($data,'CITY_ID','CITY_NAME');
		}
		
//		$data = MtsysCity::model()->findAll('PROVINCE_ID=:PID',array(':PID'=>$_POST['PROVINCE_ID']));
//		$data=CHtml::listData($data,'CITY_ID','CITY_NAME');
	
		$rs .= CHtml::tag('option',array('value'=>'0'),CHtml::encode('请选择市级'),true);
		var_dump($data);
		if(!empty($data)){
			foreach($data as $value=>$name){
				$rs .= CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
			}
		}
			
		echo $rs;
	}
	
	public function actionGetUnitCountys()
	{
		$rs = '';
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$unit_level = $user->unit_level;
		$unit_id = $user->unit_id;
		
		if($unit_level == 0 || $unit_level == 1 || $unit_level == 2){
			$data=MtsysCounty::model()->findAll('CITY_ID=:CID',
				array(':CID'=>(int)$_POST['CITY_ID']));
		}elseif($unit_level == 3){
			$data = MtsysCounty::model()->findAll('COUNTY_ID=:PID',array(':PID'=>$unit_id));		
		}elseif($unit_level == 4){
			$base = MtsysBase::model()->findByPk($unit_id);
			$data = MtsysCounty::model()->findAll('COUNTY_ID=:PID',array(':PID'=>$base->COUNTY_ID));			
		}		
		
//		$data=MtsysCounty::model()->findAll('CITY_ID=:CID',
//				array(':CID'=>(int)$_POST['CITY_ID']));
	
		$rs .= CHtml::tag('option',array('value'=>'0'),CHtml::encode('请选择区县级'),true);
		$data=CHtml::listData($data,'COUNTY_ID','COUNTY_NAME');
		if(!empty($data)){
			foreach($data as $value=>$name){
				$rs .= CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
			}
		}
	
		echo $rs;
	}
	
	public function actionGetUnitBases()
	{
		$rs = '';
		$userid = Yii::app()->user->id;
		$user = User::model()->find('id=:ID ',array(':ID'=>$userid));
		$unit_level = $user->unit_level;
		$unit_id = $user->unit_id;
		
		if($unit_level == 0 || $unit_level == 1 || $unit_level == 2 ||  $unit_level == 3){
		$data=MtsysBase::model()->findAll('COUNTY_ID=:CID',
				array(':CID'=>(int)$_POST['COUNTY_ID']));		
		}elseif($unit_level == 4){
		//	$data = MtsysBase::model()->findAll('BASE_ID=:PID',array(':PID'=>$unit_id));
			$data = User::model()->getBases($userid);	
			var_dump($data);		
		}	
		
//		$data=MtsysBase::model()->findAll('COUNTY_ID=:CID',
//				array(':CID'=>(int)$_POST['COUNTY_ID']));
	
		$rs .= CHtml::tag('option',array('value'=>'0'),CHtml::encode('请选择站点'),true);
		$data=CHtml::listData($data,'BASE_ID','BASE_NAME');
		if(!empty($data)){
			foreach($data as $value=>$name){
				$rs .= CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
			}
		}
		echo $rs;
	}	

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=BtBatterys::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
		/*
		$mUser=User::model()->findByPk(Yii::app()->user->id);
		if($mUser===null)
			throw new CHttpException(404,'The requested page does not exist.');
		else{
		switch ($model->LEVEL_N)
		{
			case 1:
				//to get 	province name
				$mProvince =  MtsysProvince::model()->findByPk($model->ORG_ID);
				$pName = $mProvince->PROVINCE_NAME;
				break;
			case 2:
				//to get city name
				$mcity =  MtsysCity::model()->findByPk($model->ORG_ID);
				$cName = $mProvince->CITY_NAME	;
				//to get 	province name
				$mProvince =  MtsysProvince::model()->findByPk($model->PROVINCE_ID);
				$pName = $mProvince->PROVINCE_NAME;
				;
				break;
			case 3:
				$mCounty =  MtsysCounty::model()->findByPk($model->ORG_ID);
				$cName = $mCounty->COUNTY_NAME	;
				//to get 	county name
				//to get city name
				$mcity =  MtsysCity::model()->findByPk($model->CITY_ID);
				$cityName = $mProvince->CITY_NAME	;
				//to get 	province name
				$mProvince =  MtsysProvince::model()->findByPk($model->PROVINCE_ID);
				$pName = $mProvince->PROVINCE_NAME;
				;
				break;
			case 4:
				$mBase =  MtsysBase::model()->findByPk($model->ORG_ID);
				$pName = $mBase->BASE_NAME;
				//to get 	base name;
				$mCounty =  MtsysCounty::model()->findByPk($model->COUNTY_ID);
				$cName = $mCounty->COUNTY_NAME	;
				//to get 	county name
				//to get city name
				$mcity =  MtsysCity::model()->findByPk($model->CITY_ID);
				$cityName = $mProvince->CITY_NAME	;
				//to get 	province name
				$mProvince =  MtsysProvince::model()->findByPk($model->PROVINCE_ID);
				$pName = $mProvince->PROVINCE_NAME;
				;
				break;
			default:
				// error
		}
		}
		
		
		return $model;
*/
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='bt-batterys-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
